switch 的简单应用

  1. 熟练判断是否瑞年
  2. 熟练使用switch case 的语法
  3. 掌握switch case 的执行入口和break结束的位置
public class YearDays {
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        int sum = 0;
        System.out.println("请输入年 月 日:\n");
        int year = cin.nextInt(), mouth = cin.nextInt(), day = cin.nextInt();
        System.out.println("您输入的年 月 日为:\n" + year + "  " + mouth + "  " + day
                + "  ");

        {
            switch (mouth)

            {
            case 12:
                sum += 30;
            case 11:
                sum += 31;
            case 10:
                sum += 30;
            case 9:
                sum += 31;
            case 8:
                sum += 31;
            case 7:
                sum += 30;
            case 6:
                sum += 31;
            case 5:
                sum += 30;
            case 4:
                sum += 31;
            case 3:
                sum += 28;
            case 2:
                sum += 31;
            case 1:
                sum += day;
                break;
            }
            if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
                if (mouth > 2)
                    sum += 1;
            System.out.println("是当年的第:" + sum + "天");
        }
    }

版权声明:本文为lxf512666原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/lxf512666/article/details/52744967