[英]Return to the top of a for-loop midway through execution?
I am trying to go back to the top a for-loop if a certain condition in an inner if-statement is fulfilled. 如果内部if语句中的某个条件得到满足,我将尝试回到for循环的顶部。 Is there any code that can do this? 是否有任何代码可以做到这一点?
continue;
跳过循环的其余部分,然后返回顶部。
Do you mean you want to use continue
? 您是说要continue
吗?
String myTraingle = "";
for (int i=1;i<=9;i++) {
myTraingle = "";
for (int j=1;j<=9;j++) {
myTraingle = myTraingle + " " + j;
if (i==j) {
System.out.println(myTraingle);
continue;
}
}
}
Output 输出量
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 9
You can use 您可以使用
continue;
And if you've got some nested loops use 如果您有一些嵌套循环,请使用
continue <label>;
Examples are here: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html 示例在这里: http : //docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html
continue;
Skips the current iteration of a for
, while
, or do-while
loop. 跳过for
, while
或do-while
循环的当前迭代。
break;
Terminates the innermost switch
, for
, while
, or do-while
statement. 终止最里面的switch
, for
, while
或do-while
语句。
Example: 例:
for(int i=0; i<100; i++) {
if(i==0) continue; // skip
if(i==10) break; // terminate
System.out.println(i);
}
// prints: 1 2 3 4 5 6 7 8 9 (one per line)
Reference: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html 参考: http : //docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html
you can use continue statement. 您可以使用Continue语句。 In every programming language including java, one can find certain condition in which loop has to iterate next iteration by discontinuing present iteration. 在包括Java在内的每种编程语言中,人们都可以找到某些条件,在这种条件下,循环必须通过中断当前迭代来迭代下一个迭代。 continue is used only for that purpose. 继续仅用于此目的。 Just write continue; 只是写继续; in inner if statement. 在内部if语句中。
When the if-statement is fulfilled, before using 'continue', overwrite count to '0'. 如果满足if语句,则在使用“继续”之前,将计数覆盖为“ 0”。
for(int i=0; i<10; i++)
{
if(/* Some Condition */)
{
i=0; // to go to top again.
continue;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.