在我的代码中,我有一个for循环,遍历一个代码的方法,直到它满足for条件。
有没有反过来打破这个for循环?
所以,如果我们看下面的代码,如果我们想打破这个for循环,当我们到“15”?
public class Test {
public static void main(String args[]) {
for(int x = 10; x < 20; x = x+1) {
System.out.print("value of x : " + x );
System.out.print("\n");
}
}
}
Outputs:
value of x : 10
value of x : 11
value of x : 12
value of x : 13
value of x : 14
value of x : 15
value of x : 16
value of x : 17
value of x : 18
value of x : 19
我试过以下没有效果:
public class Test {
public static void main(String args[]) {
boolean breakLoop = false;
while (!breakLoop) {
for(int x = 10; x < 20; x = x+1) {
System.out.print("value of x : " + x );
System.out.print("\n");
if (x = 15) {
breakLoop = true;
}
}
}
}
}
我试过一个循环:
public class Test {
public static void main(String args[]) {
breakLoop:
for(int x = 10; x < 20; x = x+1) {
System.out.print("value of x : " + x );
System.out.print("\n");
if (x = 15) {
break breakLoop;
}
}
}
}
我可以实现我想要的唯一的方式是通过打破一个for循环,我不能退出一段时间,做,如果语句。
编辑:
这只是作为一个例子,这不是我试图让它实现的代码。我现在解决了这个问题,通过在每个循环初始化后放置多个IF语句。之前,它会onlu跳出循环的一部分,由于缺乏休息;