1 package www.control;
2
3 import java.util.Scanner;
4
5 /*
6 switch 语句中的变量类型可以是 byte short int char String
7 case 标签必须为字符串常量或字面量, 一个固定的值
8 反编译 java---class(字节码文件)---反编译(IDEA),直接把class文件从浏览器copy到当前文件夹下,然后使用IDEA打开,IDEA会自动反编译成Java代码。
9 */
10
11 public class Demo3_Switch {
12 public static void main(String[] args) {
13
14 Scanner scanner = new Scanner(System.in);
15
16 String score = "";
17
18 if (scanner.hasNextLine()){
19 score = scanner.nextLine();
20 }else{
21 System.out.println("分数不合法");
22 }
23
24 //如果不用break, 则会case穿透,把后面不需要执行的语句也执行。
25 //JDK7 以后 switch 可以支持字符串
26 switch (score) {
27 case "100" :
28 System.out.println("恭喜满分");
29 break;
30 case "90" :
31 System.out.println("表现不错");
32 break;
33 case "80":
34 System.out.println("需要加油");
35 break;
36 default:
37 System.out.println("分数不合法");
38 }
39
40 scanner.close();
41 }
42 }