JAVA 输入年月返回该月份的天数

2022-07-31,,,,

import java.util.Scanner;

public class MonthDays {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter Year:");
        int year = sc.nextInt();
        System.out.print("Enter Month:");
        int month = sc.nextInt();
        int Days = 31;
        // 4,6,9,11月
        if (month == 4 || month == 6 || month == 9 || month == 11) {
            Days = 30;
        }
        // 平年或闰年二月的天数
        if (month == 2) {
            if (year % 4 != 0) {
                Days = 28;
            } else if (year % 100 == 0 & year % 400 != 0) {
                Days = 28;
            } else {
                Days = 29;
            }
        }
        System.out.println(year + "年" + month + "月有" + Days + "天。");
    }
}

输出示例:

Enter Year:2018
Enter Month:8
2018年8月有31天。

 

本文地址:https://blog.csdn.net/m0_49414969/article/details/107690582

《JAVA 输入年月返回该月份的天数.doc》

下载本文的Word格式文档,以方便收藏与打印。