switch

 1 package day03;
 2 
 3 
 4 public class SwitchDemo1 {
 5     /*    格式:
 6             switch(表达式){
 7                 case 值1:
 8                     语句体1;
 9                     break;
10                 case 值2:
11                     语句体2;
12                     break;
13                     ……
14                 default:
15                     语句体n+1;
16                     [break;]
17             }
18             1.计算表达式的值
19             2.依次和case后面的值进行比较,如果有对应的值,就会执行相应的语句。在执行过程中,遇到break会结束
20             3.如果索引的case后面的值和表达式的值都不匹配,就会执行default里面的语句体,然后结束程序。*/
21     /*eg: 根据week变量记录的数值, 程序输入对应的星期
22     例如: int week = 1;        星期一*/
23     public static void main(String[] args) {
24         int week = 1;
25         switch (week) {
26             case 1:
27                 System.out.println("星期一");
28                 break;
29             case 2:
30                 System.out.println("星期二");
31                 break;
32             case 3:
33                 System.out.println("星期三");
34                 break;
35             case 4:
36                 System.out.println("星期四");
37                 break;
38             case 5:
39                 System.out.println("星期五");
40                 break;
41             case 6:
42                 System.out.println("星期六");
43                 break;
44             case 7:
45                 System.out.println("星期天");
46             default:
47                 System.out.println("你的数据有误");
48                 break;
49         }
50     }
51 }

执行结果:

eg:

 1 package day03;
 2 
 3 import java.util.Scanner;
 4 
 5 public class Switchdemo02 {
 6     /*eg:键盘录入星期数,输出工作日、休息日(1-5)工作日,(6-7)休息日
 7     case穿透是如何产生的?
 8     如果switch语句中,case省略了break语句,就会开始case穿透
 9     现象:当开始case穿透,后续的case就不会具有匹配效果,内部的语句都会执行直到看见break,或者将整体的语句执行完毕
10     才会结束*/
11     public static void main(String[] args) {
12         Scanner sc = new Scanner(System.in);
13         System.out.println("please input weekday:");
14         int week = sc.nextInt();
15         switch (week){
16             case 1:
17             case 2:
18             case 3:
19             case 4:
20             case 5:
21                 System.out.println("workday");
22                 break;
23             case 6:
24             case 7:
25                 System.out.println("relaxday");
26                 break;
27             default:
28                 System.out.println("your data is wrong!");
29                 break;
30         }
31     }
32 }

执行结果:

 eg:

 1 package day03;
 2 
 3 import java.util.Scanner;
 4 
 5 public class TestSwitch {
 6     /*
 7         eg:键盘录入星期数,显示今天的减肥活动。
 8 
 9         周一:跑步
10         周二:游泳
11         周三:慢走
12         周四:动感单车
13         周五:拳击
14         周六:爬山
15         周日:好好吃一顿
16 
17         分析:
18             1. 键盘录入星期数据,使用变量接收
19             2. 多情况判断,采用switch语句实现
20             3. 在不同的case中,输出对应的减肥计划
21     */
22     public static void main(String[] args) {
23         Scanner sc = new Scanner(System.in);
24         System.out.println("please input data:");
25         int week = sc.nextInt();
26         switch (week){
27             case 1:
28                 System.out.println("running");
29                 break;
30             case 2:
31                 System.out.println("swimming");
32                 break;
33             case 3:
34                 System.out.println("walking");
35                 break;
36             case 4:
37                 System.out.println("riding");
38                 break;
39             case 5:
40                 System.out.println("boxing");
41                 break;
42             case 6:
43                 System.out.println("climb");
44                 break;
45             case 7:
46                 System.out.println("eating");
47                 break;
48             default:
49                 System.out.println("The content you entered is incorrect");
50                 break;
51 
52         }
53     }
54 }

执行结果:

posted @ 2020-12-26 23:57  Eleanor123  阅读(89)  评论(0编辑  收藏  举报