在Java 中,如何跳出当前的多重嵌套循环?

使用标签标注循环,使用 break 标签即可。

package constxiong.interview;

/**
 * 跳出多重循环
 * @author ConstXiong
 */
public class TestBreakMulti {

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

打印

0
1
2
3
4
5

  

来一道刷了进BAT的面试题?

原文地址:https://www.cnblogs.com/ConstXiong/p/11878795.html