在我的主要方法中是这段代码:
int hours = getHours();
这是获取 hours() 代码:
public static int getHours() {
int hours = 0;
boolean hoursNotOk = true;
do {
try {
hours = console.nextInt();
hoursNotOk = false;
}catch(Exception e) {
System.out.print(e);
}finally {
if(hoursNotOk) {
System.out.print(", please re-enter the hours again:");
}else {
System.out.print("**hours input accepted**");
}
}
}while(hoursNotOk);
return hours;
}
第一次 console.nextInt() 要求我输入,所以假设我在控制台中输入了一个“2”,它会抛出一个异常并再次循环通过 try 块但这次它没有要求我输入并不断从捕获中打印出来并最终阻止,为什么会发生这种情况?
子衿沉夜
茅侃侃
相关分类