Java 中,如何跳出当前的多重嵌套循环,循环标记(for循环,while循环)【Java面试题】

x33g5p2x  于2021-10-29 转载在 Java  
字(0.4k)|赞(0)|评价(0)|浏览(576)

一、for循环

public class Test {
    public static void main(String[] args) {
        outfor:for(int i = 0; i < 10;i++){
            for(int j = 0; j < 10;j++){
                if(j == 5){
                    break outfor;
                }
                System.out.println("j = "+j);
            }
        }
    }
}

二、while循环

public class Test {

    public static void main(String[] args) {
        int i = 0;
        outwhile:while(i < 10){
            int j = 0;
            while(j < 10){
                if(j == 5){
                    break outwhile;
                }
                System.out.println("j = "+j);
                j++;
            }
            i++;
        }
    }
}

相关文章