【筆試題】在 Java 中,如何跳出當前的多重嵌套循環?


筆試題 在 Java 中,如何跳出當前的多重嵌套循環?

public class Demo {
    public static void main(String[] args) {
        System.out.println("方法一:標號方式");
        outerloop:
        for (int i = 1; i < 5; i++) {
            for (int j = 1; j < 5; j++) {
                if (i * j > 6) {
                    System.out.println("Breaking");
                    break outerloop;
                }
                System.out.println(i + " " + j);
            }
        }
        System.out.println("Done");

        System.out.println("方法二:條件控制");
        boolean finished = false;
        for (int i = 1; i < 5 && !finished; i++) {
            for (int j = 1; j < 5; j++) {
                if (i * j > 6) {
                    System.out.println("Breaking");
                    finished = true;
                    break;
                }
                System.out.println(i + " " + j);
            }
        }
        System.out.println("Done");

        System.out.println("方法二變形:條件控制");
        for (int i = 1; i < 5; i++) {
            for (int j = 1; j < 5; j++) {
                if (i * j > 6) {
                    System.out.println("Breaking");
                    i = 5;
                    break;
                }
                System.out.println(i + " " + j);
            }
        }
        System.out.println("Done");

        System.out.println("方法三:拋出異常");
        try {
            for (int i = 1; i < 5; i++) {
                for (int j = 1; j < 5; j++) {
                    if (i * j > 6) {
                        System.out.println("Breaking");
                        throw new Exception();
                    }
                    System.out.println(i + " " + j);
                }
            }
            System.out.println("Done");// 此行代碼不會執行
        } catch (Exception e) {
            // System.out.println("e");
        }
    }
}

參考答案

``` 方法一:標號方式 1 1 1 2 1 3 1 4 2 1 2 2 2 3 Breaking Done 方法二:條件控制 1 1 1 2 1 3 1 4 2 1 2 2 2 3 Breaking Done 方法二變形:條件控制 1 1 1 2 1 3 1 4 2 1 2 2 2 3 Breaking Done 方法三:拋出異常 1 1 1 2 1 3 1 4 2 1 2 2 2 3 Breaking ```

方法一:標號方式

System.out.println("方法一:標號方式");
outerloop:
for (int i = 1; i < 5; i++) {
    for (int j = 1; j < 5; j++) {
        if (i * j > 6) {
            System.out.println("Breaking");
            break outerloop;
        }
        System.out.println(i + " " + j);
    }
}
System.out.println("Done");

參考答案

``` 方法一:標號方式 1 1 1 2 1 3 1 4 2 1 2 2 2 3 Breaking Done ```

方法二:條件控制

System.out.println("方法二:條件控制");
boolean finished = false;
for (int i = 1; i < 5 && !finished; i++) {
    for (int j = 1; j < 5; j++) {
        if (i * j > 6) {
            System.out.println("Breaking");
            finished = true;
            break;
        }
        System.out.println(i + " " + j);
    }
}
System.out.println("Done");

參考答案

``` 方法二:條件控制 1 1 1 2 1 3 1 4 2 1 2 2 2 3 Breaking Done ```
System.out.println("方法二變形:條件控制");
for (int i = 1; i < 5; i++) {
    for (int j = 1; j < 5; j++) {
        if (i * j > 6) {
            System.out.println("Breaking");
            i = 5;
            break;
        }
        System.out.println(i + " " + j);
    }
}
System.out.println("Done");

參考答案

``` 方法二變形:條件控制 1 1 1 2 1 3 1 4 2 1 2 2 2 3 Breaking Done ```

方法三:拋出異常

System.out.println("方法三:拋出異常");
try {
    for (int i = 1; i < 5; i++) {
        for (int j = 1; j < 5; j++) {
            if (i * j > 6) {
                System.out.println("Breaking");
                throw new Exception();
            }
            System.out.println(i + " " + j);
        }
    }
    System.out.println("Done");// 此行代碼不會執行
} catch (Exception e) {
    // System.out.println("e");
}

參考答案

``` 方法三:拋出異常 1 1 1 2 1 3 1 4 2 1 2 2 2 3 Breaking ```

參考資料


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM