开发技术-Java switch case 的简单用法
创始人
2024-05-24 09:47:20
0

文章目录

    • 1. integral-selector
    • 2. case
    • 3. break
    • 4. default
    • 5. 总结

最近开发写 switch 发现有的技术点还没有掌握,在此做个记录。

ON JAVA 中文版中,关于 switch 的描述为:

switch 有时也被划归为一种选择语句。根据整数表达式的值,switch 语句可以从一系列代码中选出一段去执行。它的格式如下:

        switch (integral - selector) {case integral - value1:statement;break;case integral - value2:statement;break;case integral - value3:statement;break;case integral - value4:statement;break;case integral - value5:statement;break;// ... default:statement;}

1. integral-selector

integral-selector 为能够产生整数值的表达式,或者为字符串(Java 8及以后)、枚举,具体包括 byte short char int 及它们的包装类,如果传入错误,有些代码编写工具(类似 idea)会有提示。
在这里插入图片描述

2. case

case 后面跟的是匹配条件, switch 语句匹配至第一个 case 时,开始执行后面的代码。

public class Test {public static void main(String[] args) {int a = 2;switch (a) {case 1:System.out.println("1!");case 2:System.out.println("2!");case 3:System.out.println("3!");}}
}

输出:

2!
3!

3. break

break 可使执行流程跳转至 switch 主体的末尾。这是构建 switch 语句的一种传统方式,但 break 是可选的。若省略 break, 会继续执行后面的 case 语句的代码,直到遇到一个 break 为止。

public class Test {public static void main(String[] args) {int a = 4;switch (a) {case 1:System.out.println("1!");case 2:System.out.println("2!");case 3:System.out.println("3!");break;case 4:System.out.println("4!");case 5:System.out.println("5!");break;case 6:System.out.println("6!");default:System.out.println("Default!");}}
}

输出:

4!
5!

一般来说,case 后面还是写 break 的。

4. default

如果没有匹配到 case ,就执行 default 语句。一般情况下,default 写在最后,且后面不用写 break(写了也没用)。

public class Test {public static void main(String[] args) {int a = 8;switch (a) {case 1:System.out.println("1!");case 2:System.out.println("2!");case 3:System.out.println("3!");break;case 4:System.out.println("4!");case 5:System.out.println("5!");break;case 6:System.out.println("6!");default:System.out.println("Default!");}}
}

输出:

Default!

default 没写在 switch 语句的最后面,就相当于一个普通的 case ,匹配到时就会开始执行,并不会等到匹配完所有的 case 才执行。

public class Test {public static void main(String[] args) {int a = 8;switch (a) {case 1:System.out.println("1!");case 2:System.out.println("2!");case 3:System.out.println("3!");break;default:System.out.println("Default!");case 4:System.out.println("4!");case 5:System.out.println("5!");break;case 6:System.out.println("6!");}}
}

输出:

Default!
4!
5!

这种时候在 default 后面的 break 就会有效果了。

public class Test {public static void main(String[] args) {int a = 8;switch (a) {case 1:System.out.println("1!");case 2:System.out.println("2!");case 3:System.out.println("3!");break;default:System.out.println("Default!");break;case 4:System.out.println("4!");case 5:System.out.println("5!");break;case 6:System.out.println("6!");}}
}

输出:

Default!

十般情况下也没人这样写。
在这里插入图片描述
(图源网络,侵删)

5. 总结

还是按照书本上的来吧,别整幺蛾子。

public class Test {public static void main(String[] args) {int a = 2;switch (a) {case 1:System.out.println("1!");break;case 2:System.out.println("2!");break;case 3:System.out.println("3!");break;default:System.out.println("Default!");}}
}

输出:

2!

相关内容

热门资讯

监控摄像头接入GB28181平... 流程简介将监控摄像头的视频在网站和APP中直播,要解决的几个问题是:1&...
Windows10添加群晖磁盘... 在使用群晖NAS时,我们需要通过本地映射的方式把NAS映射成本地的一块磁盘使用。 通过...
protocol buffer... 目录 目录 什么是protocol buffer 1.protobuf 1.1安装  1.2使用...
在Word、WPS中插入AxM... 引言 我最近需要写一些文章,在排版时发现AxMath插入的公式竟然会导致行间距异常&#...
【PdgCntEditor】解... 一、问题背景 大部分的图书对应的PDF,目录中的页码并非PDF中直接索引的页码...
Fluent中创建监测点 1 概述某些仿真问题,需要创建监测点,用于获取空间定点的数据࿰...
educoder数据结构与算法...                                                   ...
MySQL下载和安装(Wind... 前言:刚换了一台电脑,里面所有东西都需要重新配置,习惯了所...
修复 爱普生 EPSON L4... L4151 L4153 L4156 L4158 L4163 L4165 L4166 L4168 L4...
MFC文件操作  MFC提供了一个文件操作的基类CFile,这个类提供了一个没有缓存的二进制格式的磁盘...