switch 语句

switch 语句

switch 语句也叫开关语句,类似于闸刀开关,匹配到哪个 case 就执行对应的代码块。

基本语法

switch (表达式) {
    case 值1:
        语句块1;
        break;
    case 值2:
        语句块2;
        break;
    default:
        默认语句块;
        break;
}Code language: JavaScript (javascript)

注意事项

  • switch 表达式的类型可以是 byte、short、char、int,以及它们对应的包装类,从 JDK 7.0​ 开始还支持 String 类型
  • 每个 case 后面必须跟 break,否则会发生击穿(fall-through)——即匹配到一个 case 后会继续执行后续 case 中的代码,直到遇到 break 或 switch 结束
  • default 分支是可选的,当所有 case 都不匹配时执行

基本示例:根据数字输出月份

public static void main(String[] args) {
    int a = 6;
    switch (a) {
        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;
        case 8: System.out.println("八月"); break;
        case 9: System.out.println("九月"); break;
        case 10: System.out.println("十月"); break;
        case 11: System.out.println("十一月"); break;
        case 12: System.out.println("十二月"); break;
        default: System.out.println("输入不正确"); break;
    }
}Code language: JavaScript (javascript)

利用击穿特性:判断上半年还是下半年

多个 case 共享同一段逻辑,可以省略中间 case 的 break,让代码”穿透”到目标 case:

public static void main(String[] args) {
    int a = 2;
    switch (a) {
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:
        case 6:
            System.out.println("上半年"); break;
        case 7:
        case 8:
        case 9:
        case 10:
        case 11:
        case 12:
            System.out.println("下半年"); break;
        default:
            System.out.println("输入不正确"); break;
    }
}Code language: JavaScript (javascript)

JDK 7+ 支持 String 类型

String num = "6";
switch (num) {
    case "1":
    case "2":
    case "3":
    case "4":
    case "5":
    case "6":
        System.out.println("上半年"); break;
    case "7":
    case "8":
    case "9":
    case "10":
    case "11":
    case "12":
        System.out.println("下半年"); break;
    default:
        System.out.println("数据不正确");
}Code language: JavaScript (javascript)

switch 与 if-else if 的关系

对比项switchif-else if
判断方式等值匹配支持范围判断和等值匹配
效率等值匹配时效率更高范围判断时更灵活
能用 switch 实现的都能用 if-else if 实现,但反过来不行✅❌

总结:switch 适合单个值对比的场景(如菜单选择、状态判断),不适合范围判断(如 score >= 90)。范围判断应使用 if-else if。

练习:根据月份输出所属季节

public static void main(String[] args) {
    System.out.println("请输入月份:");
    int month = new Scanner(System.in).nextInt();

    switch (month) {
        case 3:
        case 4:
        case 5:
            System.out.println("春季"); break;
        case 6:
        case 7:
        case 8:
            System.out.println("夏季"); break;
        case 9:
        case 10:
        case 11:
            System.out.println("秋季"); break;
        case 12:
        case 1:
        case 2:
            System.out.println("冬季"); break;
        default:
            System.out.println("输入不合法,月份应为1-12");
    }
}Code language: JavaScript (javascript)

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注