P5716 月份天数

题意:输入一个年份和一个月份,判断这一年中这个月有几天 需要考虑闰年

提示:大月31天的有 1 3 5 7 8 10 12 小月30天的有4  6 9 10 2月特殊 平年28天,闰年29天

          闰年判断条件 被 400 整除的是闰年,或者是能被 4 整除,不能被 100 整除

        先定义一个月份函数,然后利用switch语句,可以起到输出的是大月还是小月

       然后再定义一个判断是否是闰年的函数

     最后输出函数

import java.util.*;
public class Main{
    private static int day(boolean isLeapYear,int month)//定义一个输入月份的函数
    {
        switch(month)
        {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                return 31;
            case 4:
            case 6:
            case 9:
            case 11:
                return 30;
            case 2:
                if(isLeapYear)
                {
                    return 29;
                }
                else
                {
                    return 28;
                }
                default:
                    return -1;
        }
    }
    private static boolean LeapYear(int year)//定义一个判断是否是闰年的函数
    {
        if(year%400==0)
        {
            return true;
        }else if(year%100==0)
        {
            return false;
        }else if(year%4==0)
        {
            return true;
        }else
        {
            return false;
        }
    }
public static void main(String[] args) {
        // TODO 自动生成的方法存根
     Scanner in=new Scanner(System.in);
     int year=in.nextInt();
     int month=in.nextInt();
     System.out.println(day(LeapYear(year),month));
  }
}

猜你喜欢

转载自www.cnblogs.com/coke-/p/12644198.html