嘿傢伙我有以下while while循環似乎不停止。它應該要求用戶一個小時。我試圖在用戶沒有輸入數字的情況下捕捉事件。while循環中的錯誤。它不會停止Java
即 輸入小時:富 你沒有輸入有效的值
應該然後允許用戶再次輸入小時值,但它一直在打印錯誤消息,並在
private static void setTime(Clock clock){
int hours = -1;
int minutes = -1;
int seconds = -1;
Scanner scanner = new Scanner(System.in);
while(true)
{
try{
System.out.print("Enter hours: ");
hours = scanner.nextInt();
}
catch(NumberFormatException nfe){
System.out.println("Input was not an integer, please try again");
continue;
}
catch(InputMismatchException ims){
System.out.println("Input was not an integer, please try again");
continue;
}
break;
}
}
謝謝,作品。那麼爲什麼它有必要在while循環中? – Tony 2011-04-09 17:11:48
所做的只是讓你有十億掃描儀。它根本沒有性能優勢。 – corsiKa 2011-04-09 17:13:36
@Tony答案在於你的問題的文檔和其他答案。你應該閱讀它們。 – krtek 2011-04-09 17:15:09