1. 基本语法
语法格式
switch(表达式){
case 常量值1:
语句块1;
break;
case 常量值2:
语句块2;
break;
case 常量值n:
语句块n;
break;
[default:
语句块n+1;
break;
]
}
执行流程图
• switch(表达式)中表达式的值必须是下述几种类型之一:byte、short、char、int、枚举、String
• case子句中的值必须是常量,不能是变量名或不确定的表达式值或范围
• 同一个switch语句,所有case子句中的常量值互不相同
• break语句用来在执行完一个case分支后程序跳出switch语句块;如果没有break,程序会顺序执行到switch结尾
• default子句是可选的,同时,位置也是灵活的。当没有匹配的case时,执行default语句
2. 应用举例
案例1:
public class SwitchCaseTest1{
public static void main(String[] args){
int num = 1;
switch(num){
case 0:
System.out.println("zero");
break;
case 1:
System.out.println("one");
break;
case 2:
System.out.println("two");
break;
case 3:
System.out.println("three");
break;
default:
System.out.println("other");
}
}
}
案例 2:
public class SwitchCaseTest2 {
public static void main(String args[]) {
String season = "summer";
switch (season) {
case "spring":
System.out.println("春暖花开");
break;
case "summer":
System.out.println("夏日炎炎");
break;
case "autumn":
System.out.println("秋高气爽");
break;
case "winter":
System.out.println("冬雪皑皑");
break;
default:
System.out.println("季节输入有误");
break;
}
}
}
错误举例:
int key = 10;
switch(key){
case key > 0 :
System.out.println("正数");
break;
case key < 0:
System.out.println("负数");
break;
default:
System.out.println("零");
break;
}
案例 3:使用 switch-case 实现:对学生成绩大于 60 分的,输出“合格”。低于
60 分的,输出“不合格”。
public class SwitchCaseTest3{
public static void main(String[] agrs){
int score = 78;
//写法一
switch(score / 10){
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
System.out.println("不合格");
break;
case 6:
case 7:
case 8:
case 9:
case 10:
System.out.println("及格");
break;
default:
System.out.println("输入成绩有误");
break;
}
//写法二
switch(score / 60){
case 0:
System.out.println("不及格");
break;
case 1:
System.out.prntln("及格");
break;
default:
System.out.print("输入成绩有误")
break;
}
}
}
3.利用case的穿透性
在 switch 语句中,如果 case 的后面不写 break,将出现穿透现象,也就是一旦
匹配成功,不会在判断下一个 case 的值,直接向后运行,直到遇到 break 或者
整个 switch 语句结束,执行终止。
案例 4:编写程序:从键盘上输入 2023 年的“month”和“day”,要求通过程序输
出输入的日期为 2023 年的第几天
import java.util.Scanner;
class SwitCaseTest4{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
System.out.println("请输入2023年的month:");
int month = scan.nextInt();
System.out.println("请输入2023年的day:");
int day = scan.nexInt();
int sumDay = 0;
switch(month){
case 12:
sumDay += 30; //这个30代表11月份的满月天数
case 11:
sumDay += 31; //这个31代表10月份的满月天数
case 10:
sumDay += 30;
case 9:
sumDay += 31;
case 8:
sumDay += 31;
case 7:
sumDay += 30;
case 6:
sumDay += 31;
case 5:
sumDay += 30;
case 4:
sumDay += 31;
case 3:
sumDay += 28;
case 2:
sumDay += 31;
case 1:
sumDay += day;
}
System.out.println(month + "月" + day + "日是2023年的第" + sumDays + "天");
scan.close();//关闭资源
}
}
2.2.4 if-else 语句与 switch-case 语句比较 • 结论:凡是使用 switch-case 的结构都可以转换为 if-else 结构。反之,不成立。
• 开发经验:如果既可以使用 switch-case,又可以使用 if-else,建议使用 switchcase。因为效率稍高。
• 细节对比:
– if-else 语句优势
• if 语句的条件是一个布尔类型值,if 条件表达式为 true 则进入分
支,可以用于范围的判断,也可以用于等值的判断,使用范围更
广。 • switch 语句的条件是一个常量值(byte,short,int,char,枚举,String),
只能判断某个变量或表达式的结果是否等于某个常量值,使用场景
较狭窄。 – switch 语句优势
• 当条件是判断某个变量或表达式是否等于某个固定的常量值时,使
用 if 和 switch 都可以,习惯上使用 switch 更多。因为效率稍高。
当条件是区间范围的判断时,只能使用 if 语句。
• 使用 switch 可以利用穿透性,同时执行多个分支,而 if…else 没有
穿透性。
• 案例:只能使用 if-else
从键盘输入一个整数,判断是正数、负数、还是零。
import java.util.Scanner;
public class IfOrSwitchDemo {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入整数:");
int num = input.nextInt();
if (num > 0) {
System.out.println(num + "是正整数");
} else if (num < 0) {
System.out.println(num + "是负整数");
} else {
System.out.println(num + "是零");
}
input.close();
} }
练习
练习 1:从键盘输入星期的整数值,输出星期的英文单词
import java.util.Scanner;
public class SwitchCaseExer1 {
public static void main(String[] args) {
//定义指定的星期
Scanner input = new Scanner(System.in);
System.out.print("请输入星期值:");
int weekday = input.nextInt();
//switch 语句实现选择
switch(weekday) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
default:
System.out.println("你输入的星期值有误!");
break;
}
input.close();
}
}
练习 2:
使用 switch 把小写类型的 char 型转为大写。只转换 a, b, c, d, e. 其它的输出
“other”。
public class SwitchCaseExer2 {
public static void main(String[] args) {
char word = 'c';
switch (word) {
case 'a':
System.out.println("A");
break;
case 'b':
System.out.println("B");
break;
case 'c':
System.out.println("C");
break;
case 'd':
System.out.println("D");
break;
case 'e':
System.out.println("E");
break;
default :
System.out.println("other");
}
} }
练习 3:
编写程序:从键盘上读入一个学生成绩,存放在变量 score 中,根据 score 的值输出
其对应的成绩等级:
score>=90 等级: A
70<=score<90 等级: B
60<=score<70 等级: C
score<60 等级: D
方式一:使用 if-else
方式二:使用 switch-case: score / 10: 0 - 10
public class SwitchCaseExer3 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("请输入学生成绩:");
int score = scan.nextInt();
char grade;//记录学生等级
//方式 1:
// if(score >= 90){
// grade = 'A';
// }else if(score >= 70 && score < 90){
// grade = 'B';
// }else if(score >= 60 && score < 70){
// grade = 'C';
// }else{
// grade = 'D';
// }
//方式 2:
switch(score / 10){
case 10:
case 9:
grade = 'A';
break;
case 8:
case 7:
grade = 'B';
break;
case 6:
grade = 'C';
break;
default :
grade = 'D';
}
System.out.println("学生成绩为" + score + ",对应的等级为" + gra
de);
scan.close();
} }
练习 4:
编写一个程序,为一个给定的年份找出其对应的中国生肖。中国的生肖基于 12 年一个
周期,每年用一个动物代表:rat、ox、tiger、rabbit、dragon、snake、horse、
sheep、monkey、rooster、dog、pig。
提示:2022 年:虎 2022 % 12 == 6
public class SwitchCaseExer4 {
public static void main(String[] args){
//从键盘输入一个年份
Scanner input = new Scanner(System.in);
System.out.print("请输入年份:");
int year = input.nextInt();
input.close();
//判断
switch(year % 12){
case 0:
System.out.println(year + "是猴年");
break;
case 1:
System.out.println(year + "是鸡年");
break;
case 2:
System.out.println(year + "是狗年");
break;
case 3:
System.out.println(year + "是猪年");
break;
case 4:
System.out.println(year + "是鼠年");
break;
case 5:
System.out.println(year + "是牛年");
break;
case 6:
System.out.println(year + "是虎年");
break;
case 7:
System.out.println(year + "是兔年");
break;
case 8:
System.out.println(year + "是龙年");
break;
case 9:
System.out.println(year + "是蛇年");
break;
case 10:
System.out.println(year + "是马年");
break;
case 11:
System.out.println(year + "是羊年");
break;
default:
System.out.println(year + "输入错误");
}
} }
练习 5:押宝游戏
随机产生 3 个 1-6 的整数,如果三个数相等,那么称为“豹子”,如果三个数之和大于
9,称为“大”,如果三个数之和小于等于 9,称为“小”,用户从键盘输入押的是“豹 子”、“大”、“小”,并判断是否猜对了
提示:随机数 Math.random()产生 [0,1)范围内的小数
如何获取[a,b]范围内的随机整数呢?(int)(Math.random() * (b - a + 1))
+ a
import java.util.Scanner;
public class SwitchCaseExer5 {
public static void main(String[] args) {
//1、随机产生 3 个 1-6 的整数
int a = (int)(Math.random()*6 + 1);
int b = (int)(Math.random()*6 + 1);
int c = (int)(Math.random()*6 + 1);
//2、押宝
Scanner input = new Scanner(System.in);
System.out.print("请押宝(豹子、大、小):");
String ya = input.next();
input.close();
//3、判断结果
boolean result = false;
//switch 支持 String 类型
switch (ya){
case "豹子": result = a == b && b == c; break;
case "大": result = a + b + c > 9; break;
case "小": result = a + b + c <= 9; break;
default:System.out.println("输入有误!");
}
System.out.println("a,b,c 分别是:" + a +"," + b +"," + c );
System.out.println(result ? "猜中了" : "猜错了");
} }
练习 6:
使用 switch 语句改写下列 if 语句:
int a = 3;
int x = 100;
if(a==1) x+=5;
else if(a==2) x+=10;
else if(a==3) x+=16;
else
x+=34;
int a = 3;
int x = 100;
switch(a){
case 1:
x += 5;
break;
case 2:
x += 10;
break;
case 3:
x += 16;
break;
default :
x += 34; }
3. 循环语句