分支控制有三種:單分支,雙分支,多分支。
單分支
基本語法:
if (條件表達式){執行代碼塊;
}
程序示例:
import java.util.Scanner;public class If01 {public static void main(String[] args) {Scanner sc = new Scanner(System.in);System.out.print("輸入你的年齡:");int age = sc.nextInt();if (age > 18) {System.out.println("你的年齡大于18。");}}
}
雙分支
基本語法:
if (條件表達式) {執行代碼塊1;
} else {執行代碼塊2;
}
程序示例:
import java.util.Scanner;public class If02 {public static void main(String[] args) {Scanner sc = new Scanner(System.in);System.out.print("輸入你的年齡:");int age = sc.nextInt();if (age > 18) {System.out.println("你已經超過18歲了。");} else {System.out.println("你還未成年。");}}
}
多分支
基本語法:
if (條件表達式1) {執行代碼塊1;
}
else if (條件表達式2) {執行代碼塊2;
}
......
else{執行代碼塊n;
}
多分支最后可以沒有 else。
如果沒有 else,當前面的所有 if 都不成立時,將什么都不執行。
switch
switch(表達式) 中的表達式數據類型要么和 case 常量中常量的類型一致,要么是可以自動類型轉換為可以進行比較的類型。
switch(表達式) 中的表達式的值的類型必須是以下類型中的一種:byte,short,int,char,enum(JKD 5 之后支持),String(JDK 7 之后支持)。
case 常量中的常量可以是常量表達式,即值為常量的表達式,如 ‘b’ + 1。case 標簽還可以是字符串或枚舉類型常量。
default 可以省略。
default 的位置隨意。
多個 case 中的值不允許重復。
case 后面如果沒有 break 將會穿透。如果多個 case 的語句體相同,則可以利用穿透來簡化代碼。
程序示例:
public static void main(String[] args) {char c = 'a';switch (c) {case 10:System.out.println("ok1");break;case 97: // 和此處匹配上System.out.println("ok2"); // 輸出 ok2break;default:System.out.println("ok3");break;}
}
程序示例:
import java.util.Scanner;public class test {public static void main(String[] args) {// 從鍵盤輸入學生成績,如果學生成績大于 60 則輸出合格,否則輸出不合格// 要求用 switch 完成System.out.println("輸入學生成績:");Scanner sc = new Scanner(System.in);int score = sc.nextInt();switch (score / 60) {case 0:System.out.println("不合格");break;case 1:System.out.println("合格");break;}}
}
程序示例:
// 這是從 JDK12 開始引入的新特性,用花括號括起來的語句可以省略 break,因為不會發生穿透現象。
public class test {public static void main(String[] args) {int number = 1;switch (number) {case 1 -> {System.out.println("一");break;}case 2 -> {System.out.println("二");break;}case 3 -> {System.out.println("三");break;}default -> {System.out.println("沒有這種選項");break;}}}
}
省略 break:
public class test {public static void main(String[] args) {int number = 1;switch (number) {case 1 -> {System.out.println("一");}case 2 -> {System.out.println("二");}case 3 -> {System.out.println("三");}default -> {System.out.println("沒有這種選項");}}}
}
用花括號括起來的語句塊只有一條語句時,可以省略花括號:
// 用花括號括起來的語句塊只有一條語句時,可以省略花括號
public class test {public static void main(String[] args) {int number = 1;switch (number) {case 1 -> System.out.println("一");case 2 -> System.out.println("二");case 3 -> System.out.println("三");default -> System.out.println("沒有這種選項");}}
}
如果多個 case 的語句體相同,則可以利用穿透來簡化代碼。未簡化的代碼:
import java.util.Scanner;public class test {public static void main(String[] args) {Scanner sc = new Scanner(System.in);System.out.print("請輸入星期數:");int number = sc.nextInt();switch (number) {case 1:System.out.println("工作日");break;case 2:System.out.println("工作日");break;case 3:System.out.println("工作日");break;case 4:System.out.println("工作日");break;case 5:System.out.println("工作日");break;case 6:System.out.println("休息日");break;case 7:System.out.println("休息日");break;default:System.out.println("輸入錯誤");break;}}
}
利用 case 穿透進行簡化:
import java.util.Scanner;public class test {public static void main(String[] args) {Scanner sc = new Scanner(System.in);System.out.print("請輸入星期數:");int number = sc.nextInt();switch (number) {case 1:case 2:case 3:case 4:case 5:System.out.println("工作日");break;case 6:case 7:System.out.println("休息日");break;default:System.out.println("輸入錯誤");break;}}
}
進一步簡化:
import java.util.Scanner;public class test {public static void main(String[] args) {Scanner sc = new Scanner(System.in);System.out.print("請輸入星期數:");int number = sc.nextInt();switch (number) {case 1, 2, 3, 4, 5:System.out.println("工作日");break;case 6, 7:System.out.println("休息日");break;default:System.out.println("輸入錯誤");break;}}
}
更進一步簡化:
import java.util.Scanner;public class test {public static void main(String[] args) {Scanner sc = new Scanner(System.in);System.out.print("請輸入星期數:");int number = sc.nextInt();switch (number) {case 1, 2, 3, 4, 5 -> System.out.println("工作日");case 6, 7 -> System.out.println("休息日");default -> System.out.println("輸入錯誤");}}
}
與所有表達式類似,switch 表達式也有一個值,可以將 switch 的結果賦值給一個變量。
程序示例:
import java.util.Scanner;public class SwitchDemo {public static void main(String[] args) {Scanner sc = new Scanner(System.in);System.out.print("請輸入一個季節:");String season = sc.next();int numLetters = switch (season) {case "spring", "summer", "winter" -> 6;case "fall" -> 4;default -> -1;};System.out.println(numLetters);}
}
執行結果:
請輸入一個季節:fall
4
switch 表達式中使用枚舉常量時,不需要為各個標簽提供枚舉名,這可以從 switch 值推導得出。
程序示例:
public class SwitchDemo2 {public static void main(String[] args) {enum Size {SMALL, MEDIUM, LARGE, EXTRA_LARGE};Size itemSize = Size.SMALL;String label = switch (itemSize) {case SMALL -> "S"; // no need to use Size.SMALLcase MEDIUM -> "M";case LARGE -> "L";case EXTRA_LARGE -> "XL";}; // 在這個例子中,完全可以省略 default, 因為每一個可能的值都有相應的一個 caseSystem.out.println(label); // S}
}
使用整數或 String 操作數的 switch 表達式必須有一個 default,因為不論操作數值是什么,這個表達式都必須生成一個值。
如果操作數為 null,會拋出一個 NullPointerException。