switch穿透性案例:月份天数查看器

package www.com.branch;

import java.util.Scanner;

//switch穿透性
public class SwitchTest01 {

//    需求: 用户输入月份可以展示该月份的天数。
//          1、3 、5、 7 、 8、 10、 12月份是 31天
//		    2月份是闰年为29天、非闰年为28天。
//          4 、6 、9、 11月份 是30天
    public static void main(String[] args) {
        System.out.println("输入月份");
        Scanner sc = new Scanner(System.in);
        int  month = sc.nextInt();

        switch (month){
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                System.out.println(month+"月是31天");
                break;
            case 2:
                System.out.println(month+"月份是闰年为29天、非闰年为28天。");
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                System.out.println(month+"月是30天");
                break;
            default:
                System.out.println("Bug");
        }
    }
}

switch穿透性把流程集中到同一处处理,简化代码。


版权声明:本文为weixin_56422984原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。