【发布时间】:2013-12-23 08:24:51
【问题描述】:
有人认为这有问题吗?第一个输入工作正常,但在第一个循环之后,它不会要求再次输入一个值。我该如何解决这个问题?
int value;
while(true)
{
Scanner scan = new Scanner(System.in);
System.out.println("Enter a value");
try
{
value = scan.nextInt();
System.out.println("value provided is: " + value);
scan.nextLine(); // consumes "\n" character in buffer
}
catch(InputMismatchException e) // outputs error message if value provided is not an integer
{
System.out.println("Incorrect input type. Try again.");
continue; // restarts while loop to allow for re-entering of a valid input
}
scan.close();
}
【问题讨论】:
-
循环何时真正停止?
-
没关系,我可以让它随时停止,但主要焦点是错误
-
这只是从我的实际代码中提取的,我只是将它设置为一个无限循环来调查这个特定的情况
-
注意,我将使用同一个扫描仪来读取整数和字符串
标签: java nosuchelementexception