switch选择结构

 

 

 1 package struct;
 2 
 3 public class SwitchDemo01 {
 4     public static void main(String[] args) {
 5         //case穿透
 6         char grade = 'C';
 7 
 8         switch(grade){
 9             case 'A':
10                 System.out.println("优秀!");
11                 break;//可选
12             case 'B':
13                 System.out.println("良好!");
14             case 'C':
15                 System.out.println("及格!");
16             case 'D':
17                 System.out.println("再接再厉!");
18             case 'E':
19                 System.out.println("挂科!");
20             default:
21                 System.out.println("未知等级!");
22         }
23     }
24 }
 1 package struct;
 2 
 3 public class SwitchDemo02 {
 4     public static void main(String[] args) {
 5         String name = "hh";
 6         //JDK7的新特性,表达式结构可以是字符串!!!
 7         //字符串的本质还是数字
 8         //反编译 java-class (字节码文件)--反编译(IDEA)
 9         switch (name){
10             case "嘿嘿":
11                 System.out.println("嘿嘿");
12                 break;
13             case "哈哈":
14                 System.out.println("哈哈");
15                 break;
16             default:
17                 System.out.println("我想吃火锅!");
18         }
19     }
20 }

 

posted on 2022-07-18 17:04  一枚努力学习的小白  阅读(17)  评论(0编辑  收藏  举报