import java.util.Scanner;
public class HelloWorldJavaMain
{
public static void main(String[] args)
{
Scanner userInputScanner = new Scanner(System.in);
System.out.println("Enter the first number.");
int inputA = userInputScanner.nextLine();
System.out.println("Enter the second number.");
int inputB = userInputScanner.nextLine();
int sumOfInputs = inputA + inputB;
System.out.println(inputA + " + " + inputB + " = " + sumOfInputs);
}
}
有人可以告诉我哪里出错了吗?
答案 0 :(得分:1)
int inputA = userInputScanner.nextLine();
应该改为
int inputA = userInputScanner.nextInt();
因为nextline没有返回int。或者你可以使用
Integer.parseInt(userInputScanner.nextLine())
与inputB
相同答案 1 :(得分:0)
使用userInputScanner.nextInt();
userInputScanner.nextLine()读取一个String行。
答案 2 :(得分:0)
Scanner userInputScanner = new Scanner(System.in);
int inputA = userInputScanner.nextInt();
int inputB = userInputScanner.nextInt();
int sumOfInputs = inputA + inputB;
System.out.println(inputA + " + " + inputB + " = " + sumOfInputs);