简体   繁体   中英

While loop with true condition in java

Will the below 'while' loop run infinitely?

 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(!isYearInCorrectFormat) {
 ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM