java语言
第一种 利用标签+break 常用
注:here要写在for循环之前,否则找不到
here
:
for (int i
=0;i
<a
.length
;i
++){
System
.out
.print("i:"+a
[i
]+" ");
for (int j
=0;j
<a
.length
;j
++){
System
.out
.println("j:"+a
[j
]+" ");
if (a
[j
] == 1){
break here
;
}
}
}
System
.out
.println("结束");
第二种 使用flag
boolean flag
= true;
for (int i
=0;i
<a
.length
&& flag
;i
++){
System
.out
.print("i:"+a
[i
]+" ");
for (int j
=0;j
<a
.length
&& flag
;j
++){
System
.out
.println("j:"+a
[j
]+" ");
if (a
[j
] == 1){
flag
= false;
}
}
}
System
.out
.println("结束");
第三种 使用try/catch跳出循环
try {
for (int i
=0;i
<a
.length
;i
++){
System
.out
.print("i:"+a
[i
]+" ");
for (int j
=0;j
<a
.length
;j
++){
System
.out
.println("j:"+a
[j
]+" ");
if (a
[j
] == 1){
throw new Exception();
}
}
}
}catch (Exception e
){
System
.out
.println("结束");
}
转载请注明原文地址: https://mac.8miu.com/read-486738.html