1. 程式人生 > >java for 跳出迴圈控制

java for 跳出迴圈控制

---碼解---

//來兩個陣列
String[] numArray = {"1", "2", "3"};
String[] strArray = {"a", "2", "c"};

 

1. continue

for (String str : strArray) {
    for (String num : numArray) {

        if (str.equals(num)){
            continue;
        }
        System.out.println(str + "---" + num);
    }
}

System.out.println(" --- end --- ");

 

控制檯 :

a---1
a---2
a---3
2---1
2---3
c---1
c---2
c---3
 --- end --- 

 

2. break

for (String str : strArray) {
    for (String num : numArray) {

        if (str.equals(num)){
            break;
        }
        System.out.println(str + "---" + num);
    }
}

System.out.println(" --- end --- ");

 

控制檯 : 

a---1
a---2
a---3
2---1
c---1
c---2
c---3
 --- end --- 

 

3. [name] : { for ....}

back:
{
    for (String str : strArray) {
        for (String num : numArray) {

            if (str.equals(num)) {
                break back;
            }
            System.out.println(str + "---" + num);
        }
    }
}
System.out.println(" --- end --- ");

back可自定義,也可以out: {} , 也可以 jump:{} 

控制檯 :

a---1
a---2
a---3
2---1
 --- end --- 

 

-----------------------------------------------------------------------------------