Java終止循環(huán)體的具體實現(xiàn)
更新時間:2014年02月15日 16:29:23 作者:
這篇文章主要介紹了Java終止循環(huán)體的具體實現(xiàn),需要的朋友可以參考下
編寫程序,是先創(chuàng)建一個字符串數組,在使用foreach語句遍歷時,如果發(fā)現(xiàn)數組中包含字符串“老鷹”則立刻中斷循環(huán)。再創(chuàng)建一個整數類型的二維數組,使用雙層foreach語句循環(huán)遍歷,當發(fā)現(xiàn)第一個小于60的數組元素,則立刻中斷整個雙層循環(huán),而不是內層循環(huán)。
復制代碼 代碼如下:
public class Foreach {
public static void main(String[] args){
System.out.println("\n-------------中斷單層循環(huán)的例子-------------");
// 創(chuàng)建數組
String[] array = new String[] { "白鷺", "丹頂鶴", "黃鸝", "鸚鵡", "烏鴉", "喜鵲",
"老鷹", "布谷鳥", "老鷹", "灰紋鳥", "老鷹", "百靈鳥" };
System.out.println("在你發(fā)現(xiàn)第一只老鷹之前,告訴我都有什么鳥。");
for (String string : array) { // foreach遍歷數組
if (string.equals("老鷹")) // 如果遇到老鷹
break;// 中斷循環(huán)
System.out.print("有:" + string+" "); // 否則輸出數組元素
}
System.out.println("\n-------------中斷雙層循環(huán)的例子-------------");
// 創(chuàng)建成績數組
int[][] myScores = new int[][] { { 67, 78, 63, 22, 66 }, { 55, 68, 78, 95, 44 }, { 95, 97, 92, 93, 81 } };
System.out.println("寶寶這次考試成績:\n數學\t語文\t英語\t美術\t歷史");
No1: for (int[] is : myScores) { // 遍歷成績表格
for (int i : is) {
System.out.print(i + "\t"); // 輸出成績
if (i < 60) { // 如果中途遇到不及格的,立刻中斷所有輸出
System.out.println("\n等等," + i + "分的是什么?這個為什么不及格?");
break No1;
}
}
System.out.println();
}
}
}
效果如圖所示:
相關文章
RestTemplate發(fā)送HTTP?POST請求使用方法詳解
這篇文章主要為大家介紹了RestTemplate發(fā)送HTTP?POST請求的使用方法詳解,有需要的朋友可以借鑒參考下希望能夠有所幫助,祝大家多多進步2022-03-03java 集合之實現(xiàn)類ArrayList和LinkedList的方法
下面小編就為大家?guī)硪黄猨ava 集合之實現(xiàn)類ArrayList和LinkedList的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10SpringBoot中使用@ControllerAdvice注解詳解
這篇文章主要介紹了SpringBoot中使用@ControllerAdvice注解詳解,@ControllerAdvice,是Spring3.2提供的新注解,它是一個Controller增強器,可對controller中被 @RequestMapping注解的方法加一些邏輯處理,需要的朋友可以參考下2023-10-10