- 方法一:使用带有标号的的break语句
public class Main {
public static void main(String[] args) {
String a1 = "";
String b1 = "";
here:
for (int i = 1; i <= 4; i++) {
a1 = "外层循环第"+i+"层";
for (int j = 1; j <= 4; j++) {
b1 = "内层循环第"+j+"层";
if (2 == j & 2 == i) {
break here;
}
}
}
System.out.println(a1+b1);
}
}
结果:
- 方法二:外层的循环条件收到内层的代码控制限制
public class Main {
public static void main(String[] args) {
String a2 = "";
String b2 = "";
Boolean state = true;
for (int i = 1; i <= 4 && state; i++) {
a2 = "外层循环第"+i+"层";
for (int j = 1; j <= 4 && state; j++) {
b2 = "内层循环第"+j+"层";
if (2 == j & 2 == i) {
state = false;
}
}
}
System.out.println(a2+b2);
}
}
结果:
- 方法三:使用try/catch强制跳出循环
public class Main {
public static void main(String[] args) {
String a3 = "";
String b3 = "";
try {
for (int i = 1; i <= 3; i++) {
a3 = "外层循环第"+i+"层";
for (int j = 1; j <= 3; j++) {
b3 = "内层循环第"+j+"层";
if (2 == j & 2 == i) {
throw new Exception();
}
}
}
} catch (Exception e) {
System.out.println(a3+b3);
}
}
}
结果: