Java開發--3--控制流程

3.1 條件結構

1、if語句邏輯

if (邏輯表達式){             //若邏輯爲true,則執行if中的語句,否則if中的語句不執行
	語句1;
	語句2;
}

2、if-else語句邏輯

if (邏輯表達式){            //若邏輯爲true,則執行if中的語句
	語句1;
}else{                    //若邏輯爲false,則執行else中的語句
	語句2;
}

代碼實例

public class test {
    public static void main (String[] agrs){
        int x = 30;
        if( x < 20 ){
            System.out.println("這是 if 語句");
        }else{
            System.out.println("這是 else 語句");          //該語句輸出
        }
    }
}

3、if-else語句嵌套

if (邏輯表達式1){            //若邏輯表達式1爲true,則執行語句1
	語句1;
}else if (邏輯表達式2){      //若邏輯表達式2爲true,則執行語句2
	語句2;
}else if (邏輯表達式3){      //若邏輯表達式3爲true,則執行語句3
	語句3;
}else{                     //以上邏輯表達式均爲false,則執行語句4
	語句4;
}

代碼實例

public class test {
    public static void main (String[] agrs){
        int x = 30;
        if( x == 10 ){
            System.out.println("X是10");
        }else if( x == 20 ){
            System.out.println("X是20");
        }else if( x == 30 ){
            System.out.println("X是30");      //該語句輸出
        }else{
            System.out.println("這是 else 語句");
        }
    }
}

4、switch-case-break結構

switch (整型表達式){     //必須是整型表達式(即int/char/long等),不能是浮點型(double)
	case 整型常量值1:    //如果整型表達式值與整型常量值1吻合,則執行該語句塊
		語句1;
		語句2;
		break;         //跳出switch結構,若不寫break則以此爲入口下面語句全都執行
	case 整型常量值2:    //同上,若匹配則執行該代碼塊。注意整型常量值不能重複
		語句3;
		語句4;
		break;        //break語句建議一直書寫
	default:          //若以上整型常量值都沒有被匹配,則執行該代碼塊。default可以省略
		語句5;
}

代碼實例

public class test {
    public static void main (String[] agrs){
        char grade = 'C';
        switch(grade){          //變量可以是byte/char/int/short/long/String等
            case 'A' :
                System.out.println("優秀");
                break;
            case 'B' :
                System.out.println("良好");
                break;
            case 'C' :
                System.out.println("及格");            //輸出結果:及格
                break;
            case 'D' :
                System.out.println("你需要再努力努力");
                break;
            default :
                System.out.println("未知等級");
        }
        System.out.println("你的等級是 " + grade);     //輸出結果:你的等級是C
    }
}

5、Java三目運算符(又稱條件運算符)

布爾表達式 ? 表達式1 : 表達式2

先算布爾表達式,若結果爲true,則計算表達式1並輸出,若結果爲false,則計算表達式2並輸出

代碼實例

public class test {
    public static void main (String[] agrs){
        int a = 100, b = 200;
        int flag = a > b ? a : b;       //a>b不成立,返回值爲b的值
        System.out.println(flag);       //輸出結果爲:200
    }
}

3.2 while循環結構

1、while語句

while (布爾表達式){       
	//先計算布爾表達式,若爲真則執行語句塊。執行完畢後再次判斷布爾表達式
	//若爲真則繼續執行語句塊。以此往復,直到布爾表達式爲假跳出while循環
	語句塊;
}

代碼實例

public class test {
    public static void main (String[] agrs){
        int age = 1;
        while (age <= 100){
            System.out.println("馬上有錢");     //輸出了100個 "馬上有錢"
            age++;
            if (age == 88){             //如果有特殊情況,也可以提前終止while循環
                break;                  //遇到break則跳出循環
            }
        }
        System.out.println("over");          //輸出100個馬上有錢之後輸出over
    }
}

2、do-while語句

do{             //當循環變量初始化與循環結束條件一樣時,通常考慮使用do-while
	語句塊 ;     //先執行語句塊
}while (布爾表達式)       
//再計算布爾表達式,若爲true則再次執行語句塊。以此往復,直到布爾表達式爲假跳出while循環

代碼實例

import java.util.Scanner;

public class test {
    public static void main (String[] agrs){
        int pwd;
        do{
            System.out.println("請輸入密碼");
            Scanner sc = new Scanner(System.in);
            pwd = sc.nextInt();       //接收用戶輸入的密碼賦予變量pwd
        }while (123 != pwd);          //輸入不等於123繼續輸入,直到輸入等於123退出
    }
}

3、while和do-while的區別

1.while循環先判斷再執行
2.do-while循環先執行一次,再判斷。因此循環體在不管任何情況下都會執行一次

3.3 for循環結構

1、for語句形式

表達式1爲"初始化表達式",表達式2爲"循環的條件",表達式3爲"循環變量的改變"

for (表達式1;表達式2(爲邏輯表達式);表達式3){        
//先計算表達式1的值,再計算表達式2的值,如果爲ture則執行循環體,否則退出循環
    
    語句塊(爲循環體)                
    //執行循環體。執行完畢之後執行表達式3。之後計算表達式2
    //如果爲true則執行循環體,否則退出循環
}

代碼實例

public class test {
    public static void main (String[] agrs){
        int sum = 0;
        
        for (int i=1; i<=100; i++){
            sum += i;                  //注意。此處是小寫i
            if (sum >= 4000){          //和while循環一樣,特殊情況可以終止循環
                break;                 //遇到break則跳出循環
            }
        }
        System.out.println(sum);      //輸出結果爲1-100的累加和:4005
    }
}

2、for的特殊格式

public class test {
    public static void main (String[] agrs){
        int sum = 0;
        int i = 1;            //原表達式1:初始化表達式
        
        for (;i <= 100;){
        //for中的表達式1和表達式3都可以不寫,但封號必須有。
        //同時此處不寫不代表絕對不寫,它只是將表達式寫到外面了。
        //注意:經測試表達式2必須有

            sum += i;
            i++;                 //原表達式3:循環變量的改變
        }
        System.out.println(sum);     //輸出結果爲1-100的累加和:5050
    }
}

3、coutinue語句和break語句

1.continue只能用於循環中。同時循環中也可以使用break語句。
2.continue作用爲跳過循環體中的剩餘語句而執行下一次循環。
3.break作用爲跳出整個循環不再執行循環,向後繼續執行。

代碼實例

public class test {
    public static void main (String[] agrs){
        int sum =0;

        for (int i=1; i<=100; i++){
            if (i%10 == 3){      //該語句表示個位爲3 的數
                break;           //若符合條件則跳出循環(本循環不再執行),進入後續代碼塊
            }
            sum += i;            //統計總和時,跳過所有個位爲3的數
        }

        for (int i=1; i<=100; i++){
            if (i%10 == 3){     //該語句表示個位爲3 的數
                continue;       //若符合條件則跳出本次循環(後面語句不再執行),進入下一次循環
            }
            sum += i;           //統計總和時,跳過所有個位爲3的數
        }
    }
}

4、for嵌套循環:以九九乘法表爲例

public class test {
    public static void main (String[] agrs){
        for(int i=1;i<=9;i++){        //代表行數。初始爲1小於等於9
            for(int j=1;j<=i;j++){    //代表列數。初始爲1小於等於i
                System.out.print(j+"*"+i+"="+i*j+" ");
            }
            System.out.println();
        }
    }
}

輸出結果

1*1=1 
1*2=2 2*2=4 
1*3=3 2*3=6 3*3=9 
1*4=4 2*4=8 3*4=12 4*4=16 
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25 
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36 
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49 
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64 
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章