代码-
for(int i = 0; i < count; i++)
{
System.out.println("Please enter a number!");
try{
numbers[i] = keyboardScanner.nextInt();
}catch(InputMismatchException ex)
{
System.out.println("You did not enter a number!");
}
}
如果您输入一个字符串,它将循环直到“计数”。为什么会出现这种情况?
最佳答案
当然,它会循环直到count
:您捕获了异常并继续循环。
当您遇到异常时,您希望break
跳出循环:
System.out.println("You did not enter a number!");
break;
关于java - 循环内的扫描仪 - 为什么输入不正确时它不退出循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13095765/