阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

switch多重选择

29次阅读
没有评论

共计 4172 个字符,预计需要花费 11 分钟才能阅读完成。

除了 if 语句外,还有一种条件判断,是根据某个表达式的结果,分别去执行不同的分支。

例如,在游戏中,让用户选择选项:

  1. 单人模式
  2. 多人模式
  3. 退出游戏

这时,switch语句就派上用场了。

switch语句根据 switch (表达式) 计算的结果,跳转到匹配的 case 结果,然后继续执行后续语句,直到遇到 break 结束执行。

我们看一个例子:

// switch
public class Main {public static void main(String[] args) {int option = 1;
        switch (option) {case 1:
            System.out.println("Selected 1");
            break;
        case 2:
            System.out.println("Selected 2");
            break;
        case 3:
            System.out.println("Selected 3");
            break;
        }
    }
}

修改 option 的值分别为123,观察执行结果。

如果 option 的值没有匹配到任何 case,例如option = 99,那么,switch 语句不会执行任何语句。这时,可以给 switch 语句加一个 default,当没有匹配到任何case 时,执行default

// switch
public class Main {public static void main(String[] args) {int option = 99;
        switch (option) {case 1:
            System.out.println("Selected 1");
            break;
        case 2:
            System.out.println("Selected 2");
            break;
        case 3:
            System.out.println("Selected 3");
            break;
        default:
            System.out.println("Selected other");
            break;
        }
    }
}

如果把 switch 语句翻译成 if 语句,那么上述的代码相当于:

if (option == 1) {System.out.println("Selected 1");
} else if (option == 2) {System.out.println("Selected 2");
} else if (option == 3) {System.out.println("Selected 3");
} else {System.out.println("Selected other");
}

对比 if ... else if 语句,对于多个 == 判断的情况,使用 switch 结构更加清晰。

同时注意,上述“翻译”只有在 switch 语句中对每个 case 正确编写了 break 语句才能对应得上。

使用 switch 时,注意 case 语句并没有花括号 {},而且,case 语句具有“穿透性 ”,漏写break 将导致意想不到的结果:

// switch
public class Main {public static void main(String[] args) {int option = 2;
        switch (option) {case 1:
            System.out.println("Selected 1");
        case 2:
            System.out.println("Selected 2");
        case 3:
            System.out.println("Selected 3");
        default:
            System.out.println("Selected other");
        }
    }
}

option = 2 时,将依次输出 "Selected 2""Selected 3""Selected other",原因是从匹配到case 2 开始,后续语句将全部执行,直到遇到 break 语句。因此,任何时候都不要忘记写break

如果有几个 case 语句执行的是同一组语句块,可以这么写:

// switch
public class Main {public static void main(String[] args) {int option = 2;
        switch (option) {case 1:
            System.out.println("Selected 1");
            break;
        case 2:
        case 3:
            System.out.println("Selected 2, 3");
            break;
        default:
            System.out.println("Selected other");
            break;
        }
    }
}

使用 switch 语句时,只要保证有 breakcase 的顺序不影响程序逻辑:

switch (option) {case 3:
    ...
    break;
case 2:
    ...
    break;
case 1:
    ...
    break;
}

但是仍然建议按照自然顺序排列,便于阅读。

switch语句还可以匹配字符串。字符串匹配时,是比较“内容相等”。例如:

// switch
public class Main {public static void main(String[] args) {String fruit = "apple";
        switch (fruit) {case "apple":
            System.out.println("Selected apple");
            break;
        case "pear":
            System.out.println("Selected pear");
            break;
        case "mango":
            System.out.println("Selected mango");
            break;
        default:
            System.out.println("No fruit selected");
            break;
        }
    }
}

switch语句还可以使用枚举类型,枚举类型我们在后面讲解。

编译检查

使用 IDE 时,可以自动检查是否漏写了 break 语句和 default 语句,方法是打开 IDE 的编译检查。

在 Eclipse 中,选择PreferencesJavaCompilerErrors/WarningsPotential programming problems,将以下检查标记为 Warning:

  • ‘switch’ is missing ‘default’ case:缺少 default 语句时警告;
  • ‘switch’ case fall-through:某个 case 缺少 break 时警告。

在 Idea 中,选择PreferencesEditorInspectionsJavaControl flow issues,将以下检查标记为 Warning:

  • ‘switch’ statement without ‘default’ branch:缺少 default 语句时警告;
  • Fallthrough in ‘switch’ statement:某个 case 缺少 break 时警告。

switch 语句存在问题时,即可在 IDE 中获得警告提示。

switch 多重选择

switch 表达式

使用 switch 时,如果遗漏了 break,就会造成严重的逻辑错误,而且不易在源代码中发现错误。从 Java 12 开始,switch 语句升级为更简洁的表达式语法,使用类似模式匹配(Pattern Matching)的方法,保证只有一种路径会被执行,并且不需要 break 语句:

// switch
public class Main {public static void main(String[] args) {String fruit = "apple";
        switch (fruit) {case "apple" -> System.out.println("Selected apple");
        case "pear" -> System.out.println("Selected pear");
        case "mango" -> {System.out.println("Selected mango");
            System.out.println("Good choice!");
        }
        default -> System.out.println("No fruit selected");
        }
    }
}

注意新语法使用 ->,如果有多条语句,需要用{} 括起来。不要写 break 语句,因为新语法只会执行匹配的语句,没有 穿透效应。

很多时候,我们还可能用 switch 语句给某个变量赋值。例如:

int opt;
switch (fruit) {case "apple":
    opt = 1;
    break;
case "pear":
case "mango":
    opt = 2;
    break;
default:
    opt = 0;
    break;
}

使用新的 switch 语法,不但不需要break,还可以直接返回值。把上面的代码改写如下:

// switch
public class Main {public static void main(String[] args) {String fruit = "apple";
        int opt = switch (fruit) {case "apple" -> 1;
            case "pear", "mango" -> 2;
            default -> 0;
        }; // 注意赋值语句要以; 结束
        System.out.println("opt =" + opt);
    }
}

这样可以获得更简洁的代码。

yield

大多数时候,在 switch 表达式内部,我们会返回简单的值。

但是,如果需要复杂的语句,我们也可以写很多语句,放到 {...} 里,然后,用 yield 返回一个值作为 switch 语句的返回值:

// yield
public class Main {public static void main(String[] args) {String fruit = "orange";
        int opt = switch (fruit) {case "apple" -> 1;
            case "pear", "mango" -> 2;
            default -> {int code = fruit.hashCode();
                yield code; // switch 语句返回值
            }
        };
        System.out.println("opt =" + opt);
    }
}

练习

使用 switch 实现一个简单的石头、剪子、布游戏。

下载练习

小结

switch语句可以做多重选择,然后执行匹配的 case 语句后续代码;

switch的计算结果必须是整型、字符串或枚举类型;

注意千万不要漏写 break,建议打开fall-through 警告;

总是写上 default,建议打开missing default 警告;

从 Java 14 开始,switch语句正式升级为表达式,不再需要 break,并且允许使用yield 返回值。

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2024-08-05发表,共计4172字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中