嘿家伙我有以下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