這個問題在這里已經有了答案: 如何打破 Java 中的嵌套回圈? (36 個回答) 4天前關閉。
我有兩個 for 回圈,在最里面的 for 回圈中我有一個開關。是否有可能在開關的一種情況下,應該中止通過最外層回圈的整個行程,以便自動轉到下一個值,例如,最外層回圈在第 1 次通過并且命令在開關,我來第二關,可以嗎?
用break它是行不通的,你有什么想法嗎?
例子:
for (){
for (){
switch(){
case 1:
//now I want to break here the inner loop, to go 1 step further, by the outer loop
}
}
// do something not wanted in case 1
}
uj5u.com熱心網友回復:
因為我不確定你認為first我給兩者都貼上了什么標簽。下面將繼續外回圈的下一次迭代。
outer:
for () {
inner:
for () {
switch() {
case 1:
continue outer; // or break inner;
}
}
}
請注意,在 的情況下break inner;,必須仍然包含標簽,因為它break;會簡單地脫離switch block.
uj5u.com熱心網友回復:
你可以使用標簽:
first :for (int i = 0; i < args.length; i ) {
second : for (int j = 0; j < args.length; j ) {
switch (j) {
case 1:
break second;// use this for breaking the second loop or use
//continue first; // to break the second and continue the first
default:
break;
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/446426.html
上一篇:使復雜性更小(更好)
