实现简单的万年历

package ForCircle;

import java.util.Scanner;
public class Day06 {

    // 万年历
    public static void timu10() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入一个年份:");
        int year = scanner.nextInt();
        System.out.println("请输入一个月份:");
        int month = scanner.nextInt();
        int week = week(year, month);
        for (int i = 0; i < 7; i++) {
            System.out.print(weekWord(i) + "\t");
        }
        System.out.println();
        // 输出该月有多少天,从一开始输出
        for (int i = 0; i <= monthDay(year, month); i++) {
            if (i == 0) {
                if(week%7!=0){
                    for (int j = 0; j < week; j++) {
                        System.out.print("\t");
                    }
                }
            }else{
                System.out.print(i + "\t");
                if ((week + i) % 7 == 0) {
                    System.out.println();
                }
            }
        }
    }

    // 用总天数来计算输入月的第一天的星期数。
    public static int week(int year, int month) {
        // 计算出多少年到多少年的总天数
        int day1 = yearDays(year, year);

        // 计算出现在这年已经过了多少天
        int day3 = passedDays(year, month);
        return (day1+day3)%7;
    }

    public static String weekWord(int a) {
        String string = "";
        switch (a) {
        case 0:
            string = "日";
            break;
        case 1:
            string = "一";
            break;
        case 2:
            string = "二";
            break;
        case 3:
            string = "三";
            break;
        case 4:
            string = "四";
            break;
        case 5:
            string = "五";
            break;
        case 6:
            string = "六";
            break;
        }
        return string;
    }

    // 返回当月的天数
    public static int monthDay(int year, int month) {
        if (month == 2) {
            return isRun(year) ? 29 : 28;
        } else {
            if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
                return 31;
            } else {
                return 30;
            }
        }
    }

    // 判断是否为闰年
    public static boolean isRun(int year) {
        if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
            return true;
        } else {
            return false;
        }
    }

    // 计算出多少年到多少年的总天数
    public static int yearDays(int beginYear, int endYear) {
        int days = 0;
        for (int i = beginYear; i < endYear; i++) {
            days += isRun(beginYear) ? 366 : 365;
        }
        return days;
    }

    // 计算出这一年已经过了多少天
    public static int passedDays(int year, int month) {
        int days = 0;
        for (int i = 1; i < month; i++) {
            switch (i) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                days += 31;
                break;
            case 2:
                days += isRun(year) ? 29 : 28;
                break;
            default:
                days += 30;
                break;
            }
        }
        System.out.println("这" + year + "年已经过了" + (days) + "天");
        return days;
    }
    public static void main(String[] args) {
        timu10();
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值