31.基础语法-switch02

32 switch02

前面演示了 switch 可以匹配 int 类型,接下来演示 switch 匹配其他数据类型

32.1 需求

需求:用switch语法改造“根据体型设定用户运动目标的逻辑”

        // 2.1 计算BMI, 并且判断体型, 设定运动目标
        String bodyType="偏瘦";

        // 2.2 设定运动目标
        String goal="";
        if (bodyType.equals("偏瘦")) {
            goal = "增肌";
        } else if (bodyType.equals("过重") || bodyType.equals("肥胖")) {
            goal = "减脂";
        } else {
            goal = "保持";
        }

        System.out.println("建议您持续:" + goal + ",加油!");

32.2 优化1

鼠标放到 chap06 行,新建 New -> Java Class -> 输入 Switch(创建包和类)

package com.itheima.chap06;

public class Switch {
    public static void main(String[] args) {
        // 2.1 计算BMI, 并且判断体型, 设定运动目标
        String bodyType="偏瘦";
        // 2.2 设定运动目标
        String goal;
        switch (bodyType){
            case "偏瘦":
                goal = "增肌";
                break;
            case "过重":  // 当匹配到"过重"或"肥胖"时,执行此代码块,直到 break 结束
            case "肥胖":
                goal = "减脂";
                break;
            default:
                goal = "保持";
                break;
        }
        System.out.println("建议您持续:" + goal + ",加油!");
    }
}

------------------------------------------------ 执行后
D:\Software\jdk17\bin\java.exe "-javaagent:D:\Software\JetBrainsIntelliJ IDEA 2025.2.4\lib\idea_rt.jar=3851" -Dfile.encoding=UTF-8 -classpath D:\Software\JavaCode\p1-basic\out\production\p1-basic com.itheima.chap06.Switch
建议您持续:增肌,加油!

Process finished with exit code 0

32.3 优化2

package com.itheima.chap06;

public class Switch {
    public static void main(String[] args) {
        // 2.1 计算BMI, 并且判断体型, 设定运动目标
        String bodyType="偏瘦";
        // 2.2 设定运动目标
        String goal;
        switch (bodyType){
            case "偏瘦" -> goal = "增肌";
            case "过重","肥胖" -> goal = "减脂";  // 使用逗号, 匹配多个值
            default -> goal = "保持";
        }
        System.out.println("建议您持续:" + goal + ",加油!");
    }
}

------------------------------------------------ 执行后
D:\Software\jdk17\bin\java.exe "-javaagent:D:\Software\JetBrainsIntelliJ IDEA 2025.2.4\lib\idea_rt.jar=3938" -Dfile.encoding=UTF-8 -classpath D:\Software\JavaCode\p1-basic\out\production\p1-basic com.itheima.chap06.Switch
建议您持续:增肌,加油!

Process finished with exit code 0

32.4 优化3

JDK 17后,switch 拥有返回值的特性,直接使用 switch 返回值赋予变量。

package com.itheima.chap06;

public class Switch {
    public static void main(String[] args) {
        // 2.1 计算BMI, 并且判断体型, 设定运动目标
        String bodyType="偏瘦";
        // 2.2 设定运动目标
        String goal = switch (bodyType) {  // switch 可以直接返回值
            case "偏瘦" -> "增肌";
            case "过重","肥胖" -> "减脂";  // 使用逗号, 匹配多个值
            default -> "保持";
        };  // 注意这里要加结束符
        System.out.println("建议您持续:" + goal + ",加油!");
    }
}

------------------------------------------------ 执行后
D:\Software\jdk17\bin\java.exe "-javaagent:D:\Software\JetBrainsIntelliJ IDEA 2025.2.4\lib\idea_rt.jar=3982" -Dfile.encoding=UTF-8 -classpath D:\Software\JavaCode\p1-basic\out\production\p1-basic com.itheima.chap06.Switch
建议您持续:增肌,加油!

Process finished with exit code 0

32.5 小结

1.switch 的使用场景?

  多判断分支的 if-else-if 结构

  判断逻辑仅仅是比较是否相等(变量类型仅支持 byte、short、int、char、String、Enum(引用的复杂类型) 等类型)

2.如何使用switch?

  case 逻辑无返回值场景:

        int bmr = 1674;
        switch(level) {
            case 0 -> bmr *= 1.2;
            case 1 -> bmr *= 1.375;
            case 2 -> bmr *= 1.55;
            default -> System.out.println("输入有误");
        };

  case 逻辑有返回值场景:JDK 17后

        // JDK 17后,switch 可以直接返回结果
        String goal = switch (bodyType) {  // switch 直接接收返回值
            case "偏瘦" -> "增肌";
            case "过重","肥胖" -> "减脂";
            default -> "保持";
        };

———————————————————————————————————————————————————————————————————————————

                                                                                                                         无敌小马爱学习

posted on 2025-12-25 11:36  马俊南  阅读(6)  评论(0)    收藏  举报