Java.Utils.caner中的NoSuchElementException异常我对Java非常陌生,但我正在阅读“Java:如何编程”(第9版)一书。已经达到了一个例子,在我的生活中,我无法弄清楚问题出在哪里。下面是教科书中源代码示例的(稍微)扩展版本:import java.util.Scanner;public class Addition {
public static void main(String[] args) {
// creates a scanner to obtain input from a command window
Scanner input = new Scanner(System.in);
int number1; // first number to add
int number2; // second number to add
int sum; // sum of 1 & 2
System.out.print("Enter First Integer: "); // prompt
number1 = input.nextInt(); // reads first number inputted by user
System.out.print("Enter Second Integer: "); // prompt 2
number2 = input.nextInt(); // reads second number from user
sum = number1 + number2; // addition takes place, then stores the total of the two numbers in sum
System.out.printf( "Sum is %d\n", sum ); // displays the sum on screen
} // end method main} // end class Addition我得到了‘NoSuchElementException’错误:Exception in thread "main" java.util.NoSuchElementExceptionat java.util.Scanner.throwFor(Scanner.java:838)
at java.util.Scanner.next(Scanner.java:1461)at java.util.Scanner.nextInt(Scanner.java:2091)
at java.util.Scanner.nextInt(Scanner.java:2050)at Addition.main(Addition.java:16)Enter First Integer:据我所知,这可能是由于源代码中的某些内容与Scanner来自java.util但我真的无法进一步推断出问题所在。
3 回答
qq_花开花谢_0
TA贡献1835条经验 获得超7个赞
NoSuchElementException
nextInt()
hasNextInt()
添加回答
举报
0/150
提交
取消