java中如何跳出多重循环,方法不止break一种

来源:互联网 发布:手机淘宝卖家中心在哪 编辑:程序博客网 时间:2024/05/21 06:46

看到网上的这道面试题都只是提供了break,不是很全面


说一下其他的两种:


1.return

public static String testOutFor() throws Exception    {        for(int i = 0 ; i < 10; i++)        {            System.out.println("i:"+i);            for(int j=0;j<5;j++)            {                System.out.println("j:"+j);                if(i==6)                {                    return "end";                }            }                    }        return null;    }


2.throw exception

public static String testOutFor() throws Exception    {        for(int i = 0 ; i < 10; i++)        {            System.out.println("i:"+i);            for(int j=0;j<5;j++)            {                System.out.println("j:"+j);                if(i==6)                {                    throw new Exception("end");                }            }                    }        return null;    }


0 1
原创粉丝点击