[英]Java - while(true) loop not checking, if condition is true, if the else statement is missing
[英]While loop with true condition in java
下面的“ 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
,它不會。
當輸入是正確的整數時,此循環將中斷。
如果正確解析變量年份,while將會中斷,但是我認為更好的做法是使用布爾變量來驗證何時停止while操作。
while(!isYearInCorrectFormat) {
...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.