1. 程式人生 > 實用技巧 >選擇結構(if switch)

選擇結構(if switch)

選擇結構

if 選擇結構

package struct;

import java.util.Scanner;

public class IfDemo {
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        System.out.println("請輸入內容:");
        String s = scanner.nextLine();
// if 單選擇結構
        if (s.equals("hello")){                 //equals:判斷字串是否相等
            System.out.println(s);
        }
        System.out.println("End");


// if 雙選擇結構
        System.out.println("請輸入成績:");
        int score01 = scanner.nextInt();

        if(score01 >= 60){
            System.out.println("及格");
        }else{
            System.out.println("不及格");
        }


// if 多選擇結構
        /**
         *  if 語句至多有一個 else 語句, else 語句必須在所有的 else if 語句之後。
         *  if 語句可以有若干個 else if 語句,它們必須在 else 語句之前。
         *  一旦其中一個 else if 語句檢測為 true ,其他的 else if 以及 else 語句都將跳過執行。
         */
        System.out.println("請輸入成績:");
        int score02 = scanner.nextInt();

        if (score02 == 100){
            System.out.println("恭喜滿分");
        }else if (score02 < 100 && score02 >= 90){
            System.out.println("A級");
        }else if (score02 < 90 && score02 >= 80){
            System.out.println("B級");
        }else if (score02 < 100 && score02 >= 70){
            System.out.println("C級");
        }else if (score02 < 100 && score02 >= 60){
            System.out.println("D級");
        }else if (score02 < 60 && score02 >= 0){
            System.out.println("不及格");
        }else{
            System.out.println("成績不合法");
        }

        scanner.close();

// if 巢狀結構
        
    }
}

switch 多選擇結構

  • 多選擇結構還有一個實現方式就是 switch case 語句。
  • switch case 語句判斷一個變數與一系值中某個值是否相等,每個值稱為一個分支。
  • switch 語句中的變數型別可以是:
    • byte、short、int、char。
    • 從 java SE 開始,switch 支援字串 String 型別了。
    • 同時 case 標籤必須為字串常量或字面量。
switch(expressioon){

		case value :

		//語句

		break;  //可選

		case value :

		//語句

		break;  //可選

	//你可以有任意數量的 case 語句

		default:  //可選

		//語句

}

case 穿透:

package struct;

import java.util.Scanner;

public class SwitchDemo01 {
    public static void main(String[] args) {
        //case穿透  //switch匹配一個具體的值
        Scanner scanner = new Scanner(System.in);
        System.out.println("請輸入等級:");
        String grade = scanner.next();

        switch (grade){
            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("未知等級");
        }
        scanner.close();
    }
}

//輸出結果:
請輸入等級:
A
優秀
良好
及格
不及格
未知等級





package struct;

import java.util.Scanner;

public class SwitchDemo01 {
    public static void main(String[] args) {
        //case穿透  //switch匹配一個具體的值
        Scanner scanner = new Scanner(System.in);
        System.out.println("請輸入等級:");
        String grade = scanner.next();

        switch (grade){
            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("未知等級");
        }
        scanner.close();
    }
}

//輸出結果:
請輸入等級:
A
優秀
良好
及格
 //JDK7的新特性,表示式結果可以是字串!!!
        //字元的本質還是數字
        //反編譯 Java---class(位元組碼檔案)---反編譯(IDEA)
        String name = "正鶴";
        switch(name){
            case "畢正鶴":
                System.out.println("畢正鶴");
                break;
            case "正鶴":
                System.out.println("正鶴");
                break;
            default:
                System.out.println("弄啥內???");
        }
    }
}

反編譯:

  1. 資料夾找到檔案位置

  1. 右擊包名,選擇 Show in Explorer

  1. 第一步中需要反編譯的檔案複製貼上到第二步的資料夾中,就可以在IDEA中看到反編譯後的檔案了