笔记
一、switch-case 分支结构
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
switch(表达式){
case 常量1:
语句1;
// break;
case 常量2:
语句2;
// break;
... ...
case 常量N:
语句N;
// break;
default:
语句;
// break;
}
二、说明
① 根据 switch 表达式中的值,依次匹配各个 case 中的常量。一旦匹配成功,则进入相应 case 结构中,调用其执行语句。当调用完执行语句以后,则仍然继续向下执行其他 case 结构中的执行语句,直到遇到 break 关键字或此 switch-case 结构末尾为止结束。
② break 关键字可以使用在 switch-case 结构中,表示一旦执行到此关键字,就跳出 switch-case 结构。
③ switch 结构中的表达式只能是如下的 6 种数据类型之一: byte、short、char、int、枚举类型(JDK5.0新增)、String类型(JDK7.0新增)。
④ case 之后只能声明常量,不能声明范围。
⑤ break 关键字是可选的,不是必要的,如果没有则程序继续执行,直到遇到遇到 break 关键字或此 switch-case 结构末尾为止结束。
⑥ default 相当于 if-else 结构中的 else, default 结构是可选的,而且位置是灵活的(可放 case 前中或者末尾)。
⑦ 如果 switch-case 结构中的多个 case 的执行语句相同,则可以考虑进行合并。
三、补充
① 凡是可以使用 switch-case 的结构,都可以转换为 if-else,反之不成立。因为 switch 的表达式有类型要求。
② 写分支结构的时候,当发现既可以使用 switch-case (同时 switch 中表达式的取值情况不太多),又可以使用 if-else 时,我们优先使用 switch-case。原因:switch-case 的执行效率稍高。
复习题
1.使用 switch 把小写类型的 char 型转为大写。只转换 a, b, c, d, e ,其他的输入 “other”。
提示: String word = scan.next(); char c = word.charAt(0); switch(c){}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import java.util.Scanner;
class SwitchTest{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("please input: ");
String word = scan.next();
char c = word.charAt(0); // 找到 word 第0位置上的字符
switch(c){
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");
}
}
}
2.对学生成绩大于 60 分得,输出“合格”。低于60分得,输入“不合格”。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import java.util.Scanner;
class GradeTest{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("please input: ");
int grade = scan.nextInt();
switch(grade / 10){
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
System.out.println("bad!");
break;
case 6:
case 7:
case 8:
case 9:
case 10:
System.out.println("well!");
break;
}
}
}
class GradeTest{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("please input: ");
int grade = scan.nextInt();
switch(grade / 60){
case 0:
System.out.println("bad!");
break;
case 1:
System.out.println("well!");
break;
}
}
}
3.根据用于指定月份,打印该月份所属的季节。
3, 4, 5 春季
6, 7, 8 夏季
9, 10, 11 秋季
12, 1, 2 冬季
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import java.util.Scanner;
class SeasonTest{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("please input: ");
int month = scan.nextInt();
switch(month){
case 3:
case 4:
case 5:
System.out.println("spring");
break;
case 6:
case 7:
case 8:
System.out.println("summer");
break;
case 9:
case 10:
case 11:
System.out.println("autumn");
break;
case 12:
case 1:
case 2:
System.out.println("winter");
break;
default:
System.out.println("error!");
break;
}
}
}
4.使用 switch 语句改写下列 if 语句:
1
2
3
4
5
6
7
8
9
10
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;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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;
break;
}