import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class LoToi1 {
public static void main(String[]args) {
// Preparing to enter
BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
System.out.println("Determines whether the year entered is a leap year.");
for(;;){
// Display messages
System.out.println("Enter an integer in the Western calendar.");
try{
// Get information to enter
String line = br.readLine();
// Convert string to numeric value
inta = Integer.parseInt(line);
// Leap year decision processing
if(a%4==0&a%100!=0||a%400==0){
System.out.println("Leap year".");
} else {//Other actions
System.out.println("Not a leap year.");
}
System.out.println("Do you want to continue?Y/N");
booleanyn=false;
while(!yn){
String input = br.readLine();
if(input.equals("Y")||input.equals("y")}
yn = true;
} else if(input.equals("N")||input.equals("n")}
yn = false;
break;
} else{
US>System.out.println("Enter Y or N.");
}
}
} catch(IOExceptione){
// Action to be taken when input/output fails
e.printStackTrace();
} catch(NumberFormatExceptione){
// Exception handling when non-numeric values are entered
System.out.println("Enter as an integer");
}
}
}
}
break in else if; why doesn't it work?
java
From the point of view of the question, I am trying to get out of the outer for()
loop with break
, but only through the most recent while(!yn)
loop.
I don't use it very often, but it's solved by using labeled break
.
public class LoToi1{
public static void main(String[]args) {
// Preparing to enter
BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
System.out.println("Determines whether the year entered is a leap year.");
outLoop:for(;;) {//label
// Display messages
System.out.println("Enter an integer in the Western calendar.");
// abbreviation
booleanyn=false;
while(!yn){
String input = br.readLine();
if(input.equals("Y")||input.equals("y")}
yn = true;
} else if(input.equals("N")||input.equals("n")}
yn = false;
break outLoop;//Break with labelling
} else{
US>System.out.println("Enter Y or N.");
}
}
// abbreviation
} // end of for (;;)
} // end of void main()
}
The reason why the loop continues when break
is placed is because
In this case, the break breaks the most recent while, and the upper for
does not break.
booleanyn
is a true or false value indicating whether to continue the large loop.
booleanyn=true;
for (;yn;) {
for
loop to be used for loop conditions.
Or
for(booleanyn=true;yn;){
as shown in
The while
loop repeats until one of the YyNn is entered, so
When you enter one of the YyNn in an infinite loop, set the true or false value to yn to get out of the loop.
while(true){
String input = br.readLine();
if(input.equals("Y")||input.equals("y")}
yn = true; // This is the default value, so it is not necessary.
break;
} else if(input.equals("N")||input.equals("n")}
yn = false;
break;
} else {
US>System.out.println("Enter Y or N.");
}
}
© 2024 OneMinuteCode. All rights reserved.