欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-10-03 14:41:05
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:"....
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