面向对象的第二次pta作业第三题:求下一天

import java.util.Scanner;


public class Main {

    static int year;
    static int month;
    static int day;
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        year = input.nextInt();
        month = input.nextInt();
        day = input.nextInt();
        
        
        if(!checkInputValidity(year, month, day)) {
            System.out.println("Wrong Format");
            System.exit(0);
        }
        
        nextDate();
        System.out.println("Next date is:"+year+"-"+month+"-"+day);
    }
    
    public static boolean isLeapYear(int year) {
        boolean y1 = year%4 == 0;
        boolean y2 = year%100 != 0;
        boolean y3 = year%400 == 0;
        
        if(y1&&y2||y3)
            return true;
        else 
            return false;
    }
    
    
    public static boolean checkInputValidity(int year,int month,int day){
        if(year>2020||year<1820||month>12||month<1||day<1||day>31) {
            return false;
        }
        switch (month) {
        case 4:case 6:case 9:case 11:
            if(day>30) 
                return false;
            break;
        case 2:
            if(isLeapYear(year)&&day>29) 
                return false;
            if(day>28) 
                return false;
            break;
        }
        return true;
    }
    
    public static void nextDate() {
        int arr[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
        if(isLeapYear(year))
            arr[2]++;
        if(day<arr[month])
            day++;
        else if(day==arr[month]&&month<12) {
            month++;
            day = 1;
        }
        else if(day==arr[month]&&month==12) {
            year++;
            month = 1;
            day = 1;
        }
}
}

 

 上一题的加强版

绞尽脑汁呜呜呜

posted on 2020-04-08 21:04  xyx's  阅读(209)  评论(0编辑  收藏  举报

导航