While solving a problem on java I am finding trouble breaking through the if statement which is inside a for loop and this for loop is inside another if statement. The code fragment is as below:
package experment;
public class myclass_2{
public static void main(String[] args){
int t;
int remaining;
boolean is_undertaker_dead = false;
Scanner s = new Scanner(System.in);
System.out.println("hey");
t = s.nextInt();
for(int i=0; i
{
String S = Integer.toString(s.nextInt());
char c[] = S.toCharArray();
for(int j=0; j
{
if(c[j]=='2')
{
remaining = c.length-j;
for(int a=j; a
{
if(remaining
{
remaining=0;
}
if(c[j+remaining]=='1')
{
is_undertaker_dead=true;
break;
}
remaining--;
}
}
}
if(is_undertaker_dead==true)
{
System.out.println("The Streak is broken!");
}
else
{
System.out.println("The streak");
}
}
}
I want to break outside of all the for loops once the break statement is called. I know this just break out of the if statement but the loop still continues. I also tried using the label method which I find on stackoverflow and google, but they somehow didn't work and confused me even more. Thank you in advance for your support, it means a lot!
解决方案
Firstly, break doesn't break out of if statement, it breaks out of loops, in your case second for loop.
In your code, break breaks out of inner for loop. To break out of outer for loop, simply put below condition, just prior to the closing bracket of the outer loop
if (is_undertaker_dead)
break;