0

编码 -

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'。为什么会这样?

4

1 回答 1

4

当然,它会循环直到count:你捕获到异常并继续循环。

break当您遇到该异常时,您想退出循环:

System.out.println("You did not enter a number!");
break;
于 2012-10-26T23:39:07.350 回答