java跳出多重循環(huán)的三種實(shí)現(xiàn)方式
java跳出多重循環(huán)的三種方式
說明:兩重for循環(huán),當(dāng) i == 2和 j == 2時(shí),就退出多重循環(huán)體,執(zhí)行后續(xù)代碼
第一種:break 配合標(biāo)簽
package cn.tedu.day04; public class Test_2020_4_9 { public static void main(String[] args) { System.out.println("三種跳出多重循環(huán)循環(huán)"); System.out.println("第一種———break 配合標(biāo)簽"); loop1: // 標(biāo)簽 for (int i = 1; i < 4; i++) { System.out.println(String.format("外層循環(huán)%d======", i)); for (int j = 1; j < 4; j++) { if (i == 2 && j == 2) { break loop1;// 跳轉(zhuǎn)標(biāo)簽 } System.out.println(String.format("\t內(nèi)層循環(huán)%d", j)); } System.out.println(String.format("%dEND=========", i)); } System.out.println("循環(huán)結(jié)束"); } }
輸出結(jié)果:
三種跳出多重循環(huán)循環(huán)
第一種———break 配合標(biāo)簽
外層循環(huán)1======
內(nèi)層循環(huán)1
內(nèi)層循環(huán)2
內(nèi)層循環(huán)3
1END=========
外層循環(huán)2======
內(nèi)層循環(huán)1
循環(huán)結(jié)束
第二種:添加判斷變量到布爾表達(dá)式中做與運(yùn)算
package cn.tedu.day04; public class Test_2020_4_9 { public static void main(String[] args) { System.out.println("三種跳出多重循環(huán)循環(huán)"); System.out.println("第二種———添加判斷變量到布爾表達(dá)式中做與運(yùn)算"); boolean variable = true; for (int i = 1; i < 4 && variable; i++) { System.out.println(String.format("外層循環(huán)%d======", i)); for (int j = 1; j < 4 && variable; j++) { if (i == 2 && j == 2) { variable = false; break; } System.out.println(String.format("\t內(nèi)層循環(huán)%d", j)); } System.out.println(String.format("%dEND=========", i)); } System.out.println("循環(huán)結(jié)束"); } }
輸出結(jié)果:
三種跳出多重循環(huán)循環(huán)
第二種———添加判斷變量到布爾表達(dá)式中做與運(yùn)算
外層循環(huán)1======
內(nèi)層循環(huán)1
內(nèi)層循環(huán)2
內(nèi)層循環(huán)3
1END=========
外層循環(huán)2======
內(nèi)層循環(huán)1
2END=========
循環(huán)結(jié)束
第三種:try catch制造異常跳出
package cn.tedu.day04; public class Test_2020_4_9 { public static void main(String[] args) { System.out.println("三種跳出多重循環(huán)循環(huán)"); System.out.println("第三種———try catch制造異常跳出"); try { for (int i = 1; i < 4; i++) { System.out.println(String.format("外層循環(huán)%d======", i)); for (int j = 1; j < 4; j++) { if (i == 2 && j == 2) { throw new Exception(); } System.out.println(String.format("\t內(nèi)層循環(huán)%d", j)); } System.out.println(String.format("%dEND=========", i)); } } catch (Exception e) { } finally { System.out.println("循環(huán)結(jié)束"); } } }
輸出結(jié)果:
三種跳出多重循環(huán)循環(huán)
第三種———try catch制造異常跳出
外層循環(huán)1======
內(nèi)層循環(huán)1
內(nèi)層循環(huán)2
內(nèi)層循環(huán)3
1END=========
外層循環(huán)2======
內(nèi)層循環(huán)1
循環(huán)結(jié)束
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Springboot通過run啟動(dòng)web應(yīng)用的方法
這篇文章主要介紹了Springboot通過run啟動(dòng)web應(yīng)用的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03關(guān)于MyBatis plus條件構(gòu)造器的逐條詳解
什么是條件構(gòu)造器呢?簡(jiǎn)單來說,條件構(gòu)造器就是用來生成我們查數(shù)據(jù)庫的sql。它可以簡(jiǎn)化sql代碼的編寫,靈活、方便且易于維護(hù)2021-09-09SpringBoot獲取yml和properties配置文件的內(nèi)容
這篇文章主要為大家詳細(xì)介紹了SpringBoot獲取yml和properties配置文件的內(nèi)容,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04Java空格替換逗號(hào)的實(shí)現(xiàn)示例
在編程中,我們經(jīng)常需要對(duì)字符串進(jìn)行各種處理,其中一個(gè)常見的需求是將字符串中的逗號(hào)替換為空格,本文主要介紹了Java空格替換逗號(hào)的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下2024-01-01使用apache 的FileUtils處理文件的復(fù)制等操作方式
這篇文章主要介紹了使用apache 的FileUtils處理文件的復(fù)制等操作方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07Springboot項(xiàng)目保存本地系統(tǒng)日志文件的實(shí)現(xiàn)方法
這篇文章主要介紹了Springboot項(xiàng)目保存本地系統(tǒng)日志文件的實(shí)現(xiàn)方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04