default

创建于 2024-12-02 / 25
字体: [默认] [大] [更大]

❮ Java 关键字


实例

如果 switch 中没有大小写匹配,请指定要运行的某些默认代码:

int day = 4;
switch (day) {
  case 6:
    System.out.println("Today is Saturday");
    break;
  case 7:
    System.out.println("Today is Sunday");
    break;
  default:
    System.out.println("Looking forward to the Weekend");
}
/ Outputs "Looking forward to the Weekend"

运行实例 »


定义和用法

default 默认关键字指定 switch 语句中的默认代码块。

default 默认关键字指定在 switch 中没有大小写匹配时要运行的代码。

注释: 如果 default 默认关键字用作开关块中的最后一条语句,则不需要break中断。


相关页面

Java 教程: Java Switch 教程


❮ Java 关键字


0 人点赞过