Java Switch Statement
Java Switch Java Switch Statement
switch语句的执行规则如下
1、从第一个case开始判断,不匹配则跳到下一个case继续判断;
2、遇到break则跳出switch语句;
3、default一般是没有匹配项才执行的,一般是放在switch语句末尾。在如下情况下,它会被执行:一、没有匹配项的时候;二、匹配项最后没有break语句,default跟在这个匹配项后面。
switch表达式支持的数据类型
=>从JDK7开始switch表达式开始支持枚举enum、String以及包装类
* byte and Byte
* short and Short
* char and Character
* int and Integer
* enum
* String
switch语句注意事项
switch(表达式)中表达式的返回值必须是下述几种类型之一:byte,short,char,int,枚举enum,String以及对应的包装类(Byte、Short、Character、Integer);
case子句中的值必须是常量不允许为变量,而且必须和switch(表达式)中表达式的数据类型一致,且所有case子句中的值应是不同的;
break语句用来在执行完一个case分支后使程序跳出switch语句块;如果没有break,程序会顺序执行到switch结尾;
default子句是可选的,当没有匹配的case时,执行default;
default子句在最后一行时,下面的break可以省略不写,但如果没有在最后一行,default子句下面需要加上break,否则执行完default后会继续执行下面的代码直到遇到break跳出循环;
实践案例
public class SwitchDemo {
public static void main(String[] args) {
int type = 4;
switch (type) {
default:
System.out.println(4);
case 1:
System.out.println(1);
case 2:
System.out.println(2);
break;
case 3:
System.out.println(3);
}
}
}
参考答案
4
1
2
```
public class SwitchDemo2 {
public static void main(String[] args) {
int x = 5;
switch (x) {
case 1:
System.out.println("A");
break;
case 2:
System.out.println("B");
break;
default:
System.out.println("结束");
case 3:
System.out.println("C");
break;
case 4:
System.out.println("D");
break;
}
}
}
参考答案
结束
C
```
public class SwitchDemo3 {
public static void main(String[] args) {
System.out.println(getValue(2));
}
public static int getValue(int i) {
int result = 0;
switch (i) {
default:
System.out.println("default");
case 1:
result = result + i;
case 2:
result = result + i * 2;
case 3:
result = result + i * 3;
}
return result;
}
}
参考答案
10
```
public class SwitchDemo4 {
public static void main(String[] args) {
byte a = 4;
switch (a) {
default:
System.out.println("default");
case 1:
System.out.println("A");
case 2:
System.out.println("B");
case 3:
System.out.println("C");
break;
case 4:
System.out.println("D");
}
}
}
参考答案
D
```
public class SwitchDemo5 {
public static void main(String[] args) {
int month = 2;
int year = 2000;
int numDays = 0;
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
numDays = 31;
break;
case 4:
case 6:
case 9:
case 11:
numDays = 30;
break;
case 2:
if (((year % 4 == 0) && !(year % 100 == 0)) || (year % 400 == 0))
numDays = 29;
else
numDays = 28;
break;
default:
System.out.println("Invalid month.");
break;
}
System.out.println("Number of Days = " + numDays);
}
}
参考答案
Number of Days = 29
```
public class SwitchDemo6 {
public static void main(String[] args) {
int number = 20;
// switch expression with int value
switch (number) {
// switch cases without break statements
case 10:
System.out.println("10");
case 20:
System.out.println("20");
case 30:
System.out.println("30");
default:
System.out.println("Not in 10, 20 or 30");
}
}
}
参考答案
20
30
Not in 10, 20 or 30
```
public class SwitchDemo7 {
public static void main(String args[]) {
// Byte age = 18;
// Short age = 18;
// Character age = 18;
Integer age = 18;
switch (age) {
case (16):
System.out.println("You are under 18.");
break;
case (18):
System.out.println("You are eligible for vote.");
break;
case (65):
System.out.println("You are senior citizen.");
break;
default:
System.out.println("Please give the valid age.");
break;
}
}
}
参考答案
You are eligible for vote.
```
参考资料
- https://www.javatpoint.com/java-switch
- https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html
- https://www.geeksforgeeks.org/switch-statement-in-java/
- https://www.baeldung.com/java-switch
Java Switch Statement的更多相关文章
- java.sql.preparedstatement和java.sql.statement的区别
本文转自CSDN,然后整理了一遍.原文出处:CSDN JDBC(java database connectivity,java数据库连接)的api中的主要的四个类之一的java.sql.stateme ...
- java.lang.ClassCastException: sun.jdbc.odbc.JdbcOdbcStatement cannot be cast to java.beans.Statement
当导入的包为:import java.sql.Statement;时,无任何错误 当导入的包为:import java.beans.Statement;时,出错
- ALTER TABLE SWITCH' statement failed. The table x' is partitioned while index 'x' is not partitioned.
1.L_Monitoring有这么些字段,ID,Collecttime,PlateType,PlateNO以及其他一些这段.建立这个表的时候是个非分区表,其中ID是主键,并在Collecttime,P ...
- Java Switch支持的类型问题
常见支持类型为int,byte,short,char及枚举类型.以上是JDK1.6以前的版本.JDK1.7时,又增加了String. 参考资料:1.java switch支持的数据类型 2.java中 ...
- Java switch case和数组
Java switch case 语句 switch case 语句判断一个变量与一系列值中某个值是否相等,每个值称为一个分支. 语法 switch case 语句格式: switch(express ...
- Java-Runoob:Java switch case
ylbtech-Java-Runoob:Java switch case 1.返回顶部 1. Java switch case 语句 switch case 语句判断一个变量与一系列值中某个值是否相等 ...
- Java Switch(String)
package shb.java.test; /** * switch支持字符串 * @Package:shb.java.test * @Description: * @author shaobn * ...
- 解决java switch……case不能匹配字符串的问题
java1.7已经支持了匹配字符串 方案1. enum Animal { dog,cat,bear; public static Animal getAnimal(String animal){ re ...
- java Switch中的case后面加上大括号({})和不加大括号的区别
java基础求真之switch 的case 后面加上大括号和不加大括号的区别. 下面给出三段代码大家看一下有什么不同以及哪段代码能够编译通过那段代码编译不能通过,为什么?(Why?) 代码片段一: i ...
随机推荐
- python并行编程学习之绪论
计算机科学的研究,不仅应该涵盖计算处理所基于的原理,还因该反映这些领域目前的知识状态.当今,计算机技术要求来自计算机科学所有分支的专业人员理解计算机处理的基础的关键,在于知道软件和硬件在所有层面上的交 ...
- bzoj 1513 POI2006 Tet-Tetris 3D 二维线段树+标记永久化
1511: [POI2006]OKR-Periods of Words Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 351 Solved: 220[S ...
- 手脱JDPack
1.PEID查壳 JDPack 2.载入OD,入口是一个pushad入栈,可以使用ESP,下硬件访问断点,shift+F9 0040E000 > pushad ; //入口 0040E001 E ...
- HTTP ------ connection 为 close 和 keep-alive 的区别
keep-alive和close这个要从TCP握手讲起 HTTP请求是基于TCP连接的,TCP的请求会包含(三次握手,中间请求,四次挥手)在HTTP/1.0时代,一个HTTP请求就要三次握手和四次挥手 ...
- Debian9(8)下python3和python2同时安装如何使用pip
在bash下Python会调用python2.x python3会调用python3.x 如果使用pip install命令安装模块会安装python2的模块. 而python3的pip命令使用的是p ...
- C++类四个默认函数&深复制&浅复制
学习C++语言的同学都知道,C++中类是有默认的几个函数的,主要是有四个函数: 四个函数 默认构造函数:A(void),无参构造函数 拷贝(复制)构造函数:A(const A&a).用一个对象 ...
- 集合框架小结-Collection
1.集合框架作为处理对象的容器存在,基本接口是Collection,相对于数组而言的话,集合框架只能存储对象,但是长度是可变的.集合框架的关系图如下: 主要的内容是list.set.map, List ...
- Shader -> Photoshop图层混合模式计算公式大全
Photoshop图层混合模式计算公式大全 混合模式可以将两个图层的色彩值紧密结合在一起,从而创造出大量的效果,在这些效果的背后实际是一些简单的数学公式在起作用. 下面是photoshop cs2中所 ...
- IIS7.5 配置应用程序初始化功能
IIS进程回收后,第一次访问会超级慢,这对于用户是不能接受的,怎么解决这个问题? 我们不能设置IIS不回收进程,因为这样可能会导致IIS内存泄漏.有效的方法时,尽量在业务空闲时间回收进程,回收后立刻预 ...
- 使用qt写的简单的图片浏览器
功能特别简单,支持png,jpg,bmp,gif文件,支持自适应窗口大小,支持放大缩小,旋转功能还有点问题,支持上下按键选择图片 因为初学qt,所以很多东西都不太会,而且c++学的不是太好,没有怎么使 ...