[英]While loop with true condition in java
Will the below 'while' loop run infinitely? 下面的“ while”循环会无限运行吗?
while (true)
try {
year = Integer.parseInt(sc.nextLine());
break;
} catch (NumberFormatException nfe) {
System.out.println("Enter valid Year");
System.out.print("try Again:-");
}
由于break
,它不会。
当输入是正确的整数时,此循环将中断。
Your while will break if you parse correctly the variable year, However I think that a better practice is to use a boolean variable to verify when should you stop the while operation. 如果正确解析变量年份,while将会中断,但是我认为更好的做法是使用布尔变量来验证何时停止while操作。
while(!isYearInCorrectFormat) {
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.