Java 作业5:判断某年某月有多少天

package Lx;
/**

  • 31天:1,3,5,7,8,10,12
  • 30天:2.4.6.9,11
  • 2月:判断闰年的公式 年()%40&&年%100!=0&||年%4000
  •  平年:28天
    
  •  闰年:29天
    
  • */
    import java.time.Year;
    import java.util.Scanner;

public class IFTest {
public static void main(String[] args) {
Scanner key = new Scanner(System.in);
System.out.println(“请输入年份:”);
//year年份
int year = key.nextInt();
System.out.println(“请输入月份:”);
//month月份
int month = key.nextInt();

   /* if (month==1||month==3||month==5||month==7||month==8||month==10||month==12){
        System.out.println(year+"年"+month+"月"+"这个月是31天");
    }else if (month==4||month==6||month==9||month==11){
        System.out.println(year+"年"+month+"月"+"这个月是30天");
    }else if (month==2){
            if( year%4==0&&year%100!=0||year%400==0){
                System.out.println(year+"年"+month+"月"+"这个月是29天");
            }else {
                System.out.println(year+"年"+month+"月"+"这个月是28天");
            }
    }else {
        System.out.println("请输入正确的月份");
    }*/
    //优化方法days天数
    int days = 0;
    if (month==1||month==3||month==5||month==7||month==8||month==10||month==12){
        days = 31;
    }else if (month==4||month==6||month==9||month==11){
        days = 30;
    }else if (month==2){
            if( year%4==0&&year%100!=0||year%400==0){
                days = 29;
            }else {
                days = 28;
            }
    }else {
        System.out.println("请输入正确的月份");
    }
    System.out.println(year+"年"+month+"月"+"这个月有"+days+"天");
    key.close();
}

}
在这里插入图片描述

发布了154 篇原创文章 · 获赞 6 · 访问量 5499

猜你喜欢

转载自blog.csdn.net/weixin_45339692/article/details/104864182