带标签的SCJP
问题描述:
以下将由于缺少标签z"而导致编译失败,但是如果我只是在o = o + 2之后将 z:移至下一个步骤,那行得通吗?这背后的逻辑是什么?
Below will be compilation fail due to "label z is missing" but if I just move z: to one step below after o = o + 2 then that will work? What is the logic behind this?
public class Breaker {
static String o = "";
public static void main(String[] args) {
z:
o = o + 2;
for (int x = 3; x < 8; x++) {
if (x == 4)
break;
if (x == 6)
break z;
o = o + x;
}
System.out.println(o);
}
}
答
您不能在代码中到处放置标签.它只能在声明之前.在这种情况下labelname: for(;;){}
这是文档
You cannot put the labels everywhere in the code. it should be only before statements. in this case labelname: for(;;){}
Here's the documentation