Java多级休息

the*_*tro 7 java loops break multi-level

我有一个构造,我在Java中for循环嵌套while循环.有没有办法调用一个break语句,使它退出for循环和while循环?

Gui*_*lmi 13

您可以使用"标记"中断.

class BreakWithLabelDemo {
public static void main(String[] args) {

    int[][] arrayOfInts = { { 32, 87, 3, 589 },
                            { 12, 1076, 2000, 8 },
                            { 622, 127, 77, 955 }
                          };
    int searchfor = 12;

    int i;
    int j = 0;
    boolean foundIt = false;

search:
    for (i = 0; i < arrayOfInts.length; i++) {
        for (j = 0; j < arrayOfInts[i].length; j++) {
            if (arrayOfInts[i][j] == searchfor) {
                foundIt = true;
                break search;
            }
        }
    }

    if (foundIt) {
        System.out.println("Found " + searchfor +
                           " at " + i + ", " + j);
    } else {
        System.out.println(searchfor
                           + " not in the array");
    }
}
Run Code Online (Sandbox Code Playgroud)

}

摘自:http: //download.oracle.com/javase/tutorial/java/nutsandbolts/branch.html