下面的'while'循环会无限运行吗?
while (true)
try {
year = Integer.parseInt(sc.nextLine());
break;
} catch (NumberFormatException nfe) {
System.out.println("Enter valid Year");
System.out.print("try Again:-");
}
3 回答
如果你正确解析变量年份,你的时间将会中断,但我认为更好的做法是使用布尔变量来验证何时应该停止while操作 .
由于
break
,它不会 .当输入是正确的整数时,此循环将中断 .