public void go(){
String o = "";
z:
for (int x = 0; x<3; x++) {
for (int y = 0; y<2;y++) {
if (x==1)
break;
if (x==2 && y==1)
break z;
o = o + x+y;
}
}
System.out.println(o);
}发布于 2014-03-26 18:22:04
它是定向 (或 )的label。请参阅下面添加的评论:
public void go(){
String o = "";
z: // <=== Labels the loop that follows
for (int x = 0; x<3; x++) {
for (int y = 0; y<2;y++) {
if (x==1)
break; // <=== Not directed, only breaks the inner loop
if (x==2 && y==1)
break z; // <=== Directed break, breaks the loop labelled with `z`
o = o + x+y;
}
}
System.out.println(o);
}发布于 2014-03-26 18:18:44
这是一个有点类似于旧的goto指令的语法。当发生中断时,您将紧跟在"z“之后退出循环,在本例中是最外层的for循环。这也适用于continue语句。
发布于 2014-03-26 18:21:26
这是个标签。您可以使用continue关键字跳过当前迭代并达到这一点,也可以跳过最里面的循环。
https://stackoverflow.com/questions/22657783
复制相似问题