有人能告诉我为什么这段代码会抛出 NoSuchElementException 吗?我以前使用扫描仪进行输入和输出,但从来没有遇到过问题。
import java.util.*;
public class TaskRunner
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the year: ");
int year = keyboard.nextInt();
System.out.print("\nEnter the month: ");
int month = (keyboard.nextInt() - 1);
System.out.print("\nEnter the day: ");
int day = keyboard.nextInt();
System.out.print(day);
}
}
由于每个人都在询问,因此在第 10 行抛出了异常。也继承了堆栈跟踪。
Enter the year: Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:907)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextInt(Scanner.java:2160)
at java.util.Scanner.nextInt(Scanner.java:2119)
at TaskRunner.main(TaskRunner.java:10)