示例代码:
public static void main(String[] args) {
anchor: // 声明锚点
for (int i = 1; i < 4; i++) {
System.out.println("1st i = " + i);
for (int j = 1; j < 10; j++) {
System.out.println("2nd j = " + j);
if (j == 2) {
System.out.println("\n");
break anchor;
}
}
}
}
栗子说明:
如果单纯的使用break
那么只是跳出当前循环,如果使用了锚点anchor
,那么就可以直接跳出指定层级的循环。