设计思想
先以字符串的形式接收数据,然后强转成整形进行运算
程序流程图
程序源码
import java.util.Scanner;
public class Addition {
public static void main( String args[] )
{
Scanner scanner=new Scanner (System.in);
String firstNumber, // first string entered by user
secondNumber; // second string entered by user
int number1, // first number to add
number2, // second number to add
sum; // sum of number1 and number2
// read in first number from user as a string
System.out.println("请输入第一个数:");
firstNumber=scanner.next();
// read in second number from user as a string
System.out.println("请输入第二个数:");
secondNumber =scanner.next();
// convert numbers from type String to type int
number1 = Integer.parseInt( firstNumber );
number2 = Integer.parseInt( secondNumber );
// add the numbers
sum = number1 + number2;
System.out.println("和为:"+sum);
System.exit( 0 ); // terminate the program
}
}
运行结果