小言_互联网的博客

Java知识梳理——如何跳出多重循环

338人阅读  评论(0)
        System.out.println("---------java中跳出多重循环的三种方式:---------");
        System.out.println("---------第一种,使用带有标号的的break语句---------");
        String a1 = "";
        String b1 = "";
        here:
        for (int i = 1; i <= 4; i++) {
            a1 = "外层循环第"+i+"层";
            for (int j = 1; j <= 4; j++) {
                b1 = "内层循环第"+j+"层";
                if (2 == j & 2 == i) {
                    break here;
                }
            }
        }
        System.out.println(a1+b1);
        System.out.println("---------第二种,外层的循环条件收到内层的代码控制限制---------");
        String a2 = "";
        String b2 = "";
        Boolean state =  true;
        for (int i = 1; i <= 4 && state; i++) {
            a2 = "外层循环第"+i+"层";
            for (int j = 1; j <= 4 && state; j++) {
                b2 = "内层循环第"+j+"层";
                if (2 == j & 2 == i) {
                    state = false;
                }
            }
        }
        System.out.println(a2+b2);
        System.out.println("---------第三种,使用try/catch强制跳出循环---------");
        String a3 = "";
        String b3 = "";
        try {
            for (int i = 1; i <= 3; i++) {
                a3 = "外层循环第"+i+"层";
                for (int j = 1; j <= 3; j++) {
                    b3 = "内层循环第"+j+"层";
                    if (2 == j & 2 == i) {
                        throw new Exception();
                    }
                }
            }
        } catch (Exception e) {
            System.out.println(a3+b3);
        }
        System.out.println("---------java中跳出多重循环的两种方式---------");

 


转载:https://blog.csdn.net/qq_38905818/article/details/101371327
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场