【发布时间】:2011-04-09 17:05:41
【问题描述】:
大家好,我有以下 while 循环,似乎不会停止。它应该询问用户一个小时。我正在尝试捕捉用户未输入数字的事件。
即 输入小时:foo 您输入的值无效
然后它应该允许用户再次输入一个小时的值,但它会一遍又一遍地打印错误消息
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;
}
}
【问题讨论】:
标签: java loops while-loop