[java]代码库
// 两数相加
import javax.swing.JOptionPane; // import class JOptionPane
public class Addition {
public static void main( String args[] )
{
String firstNumber, // first string entered by user
secondNumber; // second string entered by user
double number1, // first number to add
number2, // second number to add
sum; // sum of number1 and number2
// 从对话框中输入第一个数;对话框中输入的都是string类型的值
firstNumber =
JOptionPane.showInputDialog( "请输入第个数:" ); //对话框
// 从对话框中输入第二个数
secondNumber =
JOptionPane.showInputDialog( "Enter second integer" ); //对话框
// 将string类型的数据强制转换为int类型
number1 = Double.parseDouble( firstNumber );
number2 = Double.parseDouble( secondNumber );
// 两数相加
sum = number1 + number2;
// 输出结果对话框,后边两个参数与标题框有关,可有可无;
JOptionPane.showMessageDialog(
null, "The sum is " + sum, "Results",
JOptionPane.PLAIN_MESSAGE );
System.exit( 0 );
}
}
[代码运行效果截图]