package com;
import java.util.Scanner;
/*
 * 动态日历:
 *   1. 从控制台输入年和月,在控制天显示当前月的日历
 * */
public class Demo17 {
    public static void main(String[] args) {
        // 1.从控制台输入年和月
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入年 月:");
        int year = scanner.nextInt();
        int month = scanner.nextInt();
        // 2.根据年和月计算当月有多少天
        int dayOfMonth = getDayOfMonth(year, month);
        // 3.根据年和月计算出当月1号星期几 1900年1月1日 星期一  1905 3 1
        int historyDay = 0;
        historyDay = (year - 1900) * 365;
        for (int i = 1900; i < year; i++) {
            if (isLeapYear(i)) {
                historyDay++;
            }
        }
        for (int i = 1; i < month; i++) {
            historyDay += getDayOfMonth(year, i);
        }
        int week = historyDay % 7;
        // 4.根据当月天数和1号的星期只输出日历
        System.out.println("星期一\t星期二\t星期三\t星期四\t星期五\t星期六\t星期天");
        for (int j = 0; j < week; j++) {
            System.out.print("\t\t");
        }
        for (int i = 1; i <= dayOfMonth; i++) {
            System.out.print(i + "\t\t");
            if ((i + week) % 7 == 0) {
                System.out.println();
            }
        }
    }
    /*
    方法的定义:
    访问修饰符 返回值类型 方法名(参数列表...) {
        方法体
        return 返回值; // 返回结果,当代码执行到return时,会直接结束此方法。后续的代码不会再执行。
    }
    1. 返回可以是任意类型,如果不需要返回值则写 void
    方法:
        1.主要用于封装重复代码
    方法的调用:
        1. 方法名(参数...);
    * */
    // 判定某一年是否是闰年
    public static boolean isLeapYear(int year) {
        return year % 400 == 0 || year % 4 == 0 && year % 100 != 0;
    }
    // 得到一个月有多少天
    public static int getDayOfMonth(int year, int month) {
        int dayOfMonth;
        switch (month) {
            case 2:
                dayOfMonth = 28;
                if (isLeapYear(year)) {
                    dayOfMonth = 29;
                }
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                dayOfMonth = 30;
                break;
            default:
                dayOfMonth = 31;
                break;
        }
        return dayOfMonth;
    }
}