--------------------------------------------------------------------------------
break範例
--------------------------------------------------------------------------------
class BreakWithLabelDemo {
    public static void main(String[] args) {

        int[][] arrayOfInts = {
            { 32, 87, 3, 589 },
            { 12, 1076, 2000, 8 },
            { 622, 127, 77, 955 }
        };
        int searchfor = 12;

        int i;
        int j = 0;
        boolean foundIt = false;

    search: // 迴圈名稱可自定, 要符合Java命名規則, 迴圈名稱與迴圈宣告之間不能插入Java指令, 迴圈名稱也可寫在迴圈宣告左邊
        for (i = 0; i < arrayOfInts.length; i++) {
            for (j = 0; j < arrayOfInts[i].length; j++) {
                if (arrayOfInts[i][j] == searchfor) {
                    foundIt = true;
                    break search; // break指令只能中斷把break指令包圍起來的迴圈
                }
            }
        }

        if (foundIt) {
            System.out.println("Found " + searchfor + " at " + i + ", " + j);
        } else {
            System.out.println(searchfor + " not in the array");
        }
    }
}


--------------------------------------------------------------------------------
continue範例
--------------------------------------------------------------------------------
class ContinueWithLabelDemo {
    public static void main(String[] args) {

        String searchMe = "Look for a substring in me";
        String substring = "sub";
        boolean foundIt = false;

        int max = searchMe.length() - substring.length();

    test:
        for (int i = 0; i <= max; i++) {
            int n = substring.length();
            int j = i;
            int k = 0;
            while (n-- != 0) {
                if (searchMe.charAt(j++) != substring.charAt(k++)) {
                    continue test;   //先跳到test迴圈結束點{ 再從頭開始
                }
            }
            foundIt = true;
                break test;
        }
        System.out.println(foundIt ? "Found it" : "Didn't find it");
    }
}

 

考古題

1. What is the value of i at line 10 ?

Click the exhibit button:
1. public class Test {
2.  public static void main(String args[]) {
3.   int i= 0;
4.   while (i){
5.    if (i==4) {
6.     break;
7.    }
8.    ++i;
9.   }
10.
11.   }
12.}

click 按
exhibit 展示,陳列
button 按鈕
value 值
A. 0
B. 3
C. 4
D. 5
E. the code will not compile

My ans: C

答案:

E 因為while (i) 小括號內()必須是boolean或判斷式 i.e. while(i<5) or while(true)

++i = 加1後重新執行迴圈
 

 

2.What is the result ?

Given:
1. public class ForBar {
2. public static void main(String []args){
3.  int i = 0, j = 5;
4.   tp: for ( ; ; ) {   
5.    i ++;
6.    for(;;)
7.    if(i > --j) break tp;                                             //其實內迴圈就是第6第7行一直跑 直到滿足i>--j滿足條件 就直接印出答案  i++根本不會再有機會執行
8.   }
9.   System.out.println("i = " + i + ", j = "+ j);
10. }
11.}

 

result 結果
program 程式
error 錯誤
cause 導致, 引起
compilation 編譯
fail 失敗


A. The program runs and prints "i=1, j=0"
B. The program runs and prints "i=1, j=4"
C. The program runs and prints "i=3, j=4"
D. The program runs and prints "i=3, j=0"
E. An error at line 4 causes compilation to fail 
F. An error at line 7 causes compilation to fail 

My ans: F

答案: A

 

3. After execution, what are the values for i and j?

Given:
1. int i= 1, j= 10 ;
2. do {
3.     if (i++> --j) continue;
4. } while (i<5);

 

execution 執行


A. i = 6 and j= 5
B. i = 5 and j= 5
C. i = 6 and j= 4
D. i = 5 and j= 6
E. i = 6 and j= 6

答案: D

3.     if (i++> --j) continue;
4. } while (i<5);

i++(後加)>(先減)--j
所以其實是

i> --j       Boolean i++ i<5
1> 9 False 2 True
2> 8 False 3 True
3> 7 False 4 True
4> 6 False 5 False


 

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 鈴木保齡球 的頭像
    鈴木保齡球

    Java程式學習手札

    鈴木保齡球 發表在 痞客邦 留言(0) 人氣()