zoukankan      html  css  js  c++  java
  • 运算符和表达式 、 分支结构 输入年份和月份,输出该月的天数(使用switch-case)

    思路:三个板块,A.二月比较特殊,平年的二月只有28天,而闰年的二月有 29 天;

            B.4、6、9、11月;

            C.其他1、3、5、7、8、10、12月。

    import java.util.Scanner;
    public class DayOfMonth {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入年份(例如:2012)");
            int year = scanner.nextInt();
            System.out.println("请输入月份(例如:1)");
            int month = scanner.nextInt();
            scanner.close();
            // 某月的天数
            int days = 0;
            switch (month) {
            case 2:
                // 判断是否为闰年,闰年29天,非闰年28天
                if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
                    days = 29;
                } else {
                    days = 28;
                }
                break;
            // 4,6,9,11为小月
            case 4:
            case 6:
            case 9:
            case 11:
                days = 30;
                break;
            // 其余为大月
            default:
                days = 31;
            }
            System.out.println(year + "年" + month + "月有" + days + "天");
        }
    }
  • 相关阅读:
    内存溢出
    接手新业务
    pjb fabu
    中文手册
    人背的时候,做啥都失败
    帮助开发人员学习
    python中的__dict__,__getattr__,__setattr__
    NetCore在Docker中发布及运行
    ELK基础配置
    IdentityServer4 手动验签及日志记录
  • 原文地址:https://www.cnblogs.com/luckyBrown/p/5840511.html
Copyright © 2011-2022 走看看