天天看點

java選擇結構和循環結構注意項總結

一、選擇結構

if

if…else…

if …else if…else…

switch…case…

package test;

public class test1 {
    public static void main(String[] args) {
        example1();//if
        example11();//三元運算
        example2();//if  else
        example3();//if... else if... else
        example4();//switch...case..
    }

    public static void example1() {
        int a = 3;
        if (a > 2) {     //if小括号括号裡表示條件,為布爾值,true時執行大括号的内容
            a--;
            System.out.println(a);
        }
    }

    public static void example11() {
        int a = 3;
        int c = (3 < 2) ? a++ : a--;
        System.out.println(c);
        //轉換成三元運算,于對某個變量進行指派,當判斷條件成立時
        // 運算 結果為分号左邊的值,否則結果為分号右邊的值
    }

    public static void example2() {
        int a = 3;
        if (a < 2) {
            a--;
        } else {    //一樣是判斷小括号裡的布爾值,若true執行第一個大括号,否則執行下一個大括号
            ++a;
        }
        System.out.println(a);
    }

    public static void example3() {
        double a = 86.3;
        if (a > 90 & a < 100) {        //多條件判斷所用的結構形式
            System.out.println("A級");
        } else if (a > 80 & a < 90) {
            System.out.println("B級");
        } else if (a > 70 & a < 80) {
            System.out.println("C級");
        } else {
            System.out.println("不及格");
        }
    }

    public static void example4() {
        int number = 3;           //隻能針對某一個條件做出判斷
        switch (number) {         //switch語句中的表達式隻能是byte、short、 char、int、
            // 枚舉(JDK1.5引入的)、String類型(JDK1.7引入的) 的值
            case 1:
                System.out.println("0号");
                break;
            case 2:
                System.out.println("1号");
                break;
            case 3:
                System.out.println("2号");
                break;
            default:
                System.out.println("無此号數");
        }
    }

}      

二、循環結構

while

do…while

for

循環嵌套

跳轉(break、continue)

package test;

public class test1 {
    public static void main(String[] args) {
        example1();//  while
        example2();//   do...while
        example3();//   for
        example4();//  循環嵌套
        example5();//  break
        example6();//  continue
    }

    public static void example1() {
        int a = 3;
        while (a < 9) {
            a += 1;
            System.out.println(a);
            //while循環語句會反複地進行條件判斷,
            // 隻要條 件成立,{}内的執行語句就會執行,直到條件不成立,while循環結束
        }
    }

    public static void example2() {
        int a = 3;
        do {            //   與第一種相似,僅僅差別在先寫執行語句再寫循環條件
            a += 1;
            System.out.println(a);
        } while (a < 9);

    }


    public static void example3() {
        int a;
        for (a = 3; a < 9; a += 1) {   //  for(初始表達式;循環條件;操作表達式)
            System.out.println(a);
        }
    }

    public static void example4() {  //  循環嵌套,九九乘法表
        for (int a = 1; a <= 9; a++) {     //需要注意  先執行a=1和a<=9判斷,然後執行下一個for循環
            //直到第二個for結束時才執行a++,再執行a<=9.
            for (int b = 1; b <= a; b++) {
                System.out.print(b + "*" + a + "=" + a * b + '\t');
            }
            System.out.println();
        }
    }

    public static void example5() {
        int a = 3;
        while (a < 9) {
            a += 1;
            if (a == 7) {
                break;//  break是退出整個循環,一旦用了break,目前循環過後不再進行
            }
            System.out.println(a);
        }
        System.out.println("已經找到比對成功的數,可退出");
    }

    public static void example6() {
        int a=3;
        while (a<9){
            a+=1;
            if (a==7){
                System.out.println("檢驗出此數有誤,已排查,請繼續");
                continue;   //  注意continue是隻退出目前的一個循環過程,等到下個循環還是會繼續的
            }
            System.out.println(a);
        }

    }

    {

    }

    {

    }
}