如何打破嵌套 for 循环中 switch 中最外层的 for 循环?

how do I break the outermost for-loop in a switch in a nested for-loop?

我有两个 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
}

您可以使用标签:

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;
        }
    }
}

因为我不确定你的想法是什么 first 我把两者都标记了。下面继续外循环的下一次迭代

outer:
for () {
   inner:
   for () {
       switch() {
       case 1:
         continue outer;  // or break inner;
       }
   }
}

请注意,在 break inner; 的情况下,仍必须包含标签,因为 break; 会简单地从 switch block 中跳出。