JAVA 基礎知識點複習(四)三大控制結構


這系列博文主要是將自己之前學習的一些java基礎內容進行歸納與總結。

順序結構

從上至下,從左至右

選擇結構

  • (1) if…else
if (true) {}

if (true) {} else {}

if (true) {} else if (true) {} else {}
  • (2) switch
switch (1) {
    case 1:
        System.out.println("A");
        break;
    case 2:
        System.out.println("B");
        break;
    default:
        System.out.println("C");
}

switch 後可以跟char、byte、short、int、Character、Byte、Short、Integer、String(jdk1.7後支持)、enum

如果沒有遇到break或return,則從第一個case匹配到後一直往下執行

如果switch後是枚舉類,則case後面必須要是枚舉枚舉常量的非限定名稱

switch (weekEnum){
    case WeekEnum.MONDAY: // 編譯異常 An enum switch case label must be the unqualified name of an enumeration constant
    case TUESDAY: // 正確寫法

如果switch後是null,則會報空指針異常

String str = null;
switch (str){ // 'NullPointerException'
}

循環結構

  • (1) while
while (true) {}
do {} while (true); // 最少執行一次方法體
  • (2) for
for (int i=0; i<10; i++) {
    // do something
}

while循環控制的變量作用於整個方法,而for循環控制的變量只作用於for循環

循環嵌套

    public static void main(String[] args) {
        int row = 4;
        t(row);
        t1(row);
        t2(row);
        t3(row);
        t4(row);
        t5(row);
    }

    /**
     * 列隨着行的增多而增多
     * *
     * **
     * ***
     * ****
     */
    private static void t(int row){

        for (int i = 0; i < row; i++) {
            for (int j = 0; j <= i; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }

    /**
     * 列隨着行的增多而減少
     * ****
     * ***
     * **
     * *
     */
    private static void t1(int row){

        for (int i = 0; i < row; i++) {
            for (int j = 0; j < row -i; j++) {
                System.out.print("*");
            }
            System.out.println();

        }
    }

    /**
     * 列隨着行的增多而增多,並且需要控制空格站位
     *    *
     *   **
     *  ***
     * ****
     */
    private static void t2(int row){

        for (int i = 0; i < row; i++) {
            for (int j = 0; j < row-i; j++) {
                System.out.print(" ");
            }
            for (int j = 0; j <= i; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }

    /**
     * 列隨着行的增多而減少,並且需要控制空格站位
     * ****
     *  ***
     *   **
     *    *
     */
    private static void t3(int row){

        for (int i = 0; i < row; i++) {
            for (int j = 0; j <= i; j++) {
                System.out.print(" ");
            }
            for (int j = 0; j < row -i; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }

    /**
     * 範圍從小到大,掌握中值
     *    *
     *   ***
     *  *****
     * *******
     */
    private static void t4(int row){
        int column = 2 * row -1;
        int median = column / 2;
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < column; j++) {
                if (j >= median - i && j <= median + i) {
                    System.out.print("*");
                }else {
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
    }

    /**
     * 範圍從大到小
     * *******
     *  *****
     *   ***
     *    *
     */
    private static void t5(int row){
        int column = 2 * row -1;
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < column; j++) {
                if (j >= i && j< column - i) {
                    System.out.print("*");
                }else {
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
    }

break和continue

break:終止循環,只作用於switch和循環語句(for/while),如果存在循環嵌套則只作用於當前循環。

continue:跳出當前循環,進入下次循環,只作用於循環語句,如果存在循環嵌套則只作用於當前循環。

break和continue都可以通過標識符來控制所需結束的循環

outer:
for (int i = 0; i < 3; i++) {
    inner:
    for (int j = 0; j < 3; j++) {

        for (int k = 0; k < 3; k++) {
            break outer; // 直接結束最外層循環
        }
    }
}

無限循環的兩種寫法

for (; ; ) {}

while (true) {}

趣味題

打印所有水仙花數(一個 3 位數,它的每個位上的數字的3次冪之和等於它本身)

    public static void main(String[] args) {
        for (int i = 100; i < 1000; i++) {
            int n = (i / 100);
            int m = (i / 10 % 10);
            int k = (i % 10);
            int multiply = (int) (Math.pow(n, 3) + Math.pow(m, 3) + Math.pow(k, 3));
            if (multiply == i) {
                System.out.println(i);
            }
        }
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章