Java练习题:控制语句_打印日历

1、题目

已知1900年1月1日是星期一,请用户输入查询的年份以及月份,查询出对应的万年历,示例如下图所示:

 

2、代码

import java.util.Scanner;

public class Test { // 类名要与文件名保持一致
    public static void main(String[] args) {
        System.out.println("********** 欢迎使用万年历 **********");
        // 创建Scanner类型的对象 input 使用关键字new来创建对象,System.in 输入流 指代输入设备
        Scanner input = new Scanner(System.in);
        System.out.print("请输入查询的年份:");
        int year = input.nextInt(); // 后期优化:只能输入大于1900的年份且为整数
        System.out.print("请输入查询的月份:");
        int month = input.nextInt();
        input.close();
        // 总天数
        int sumDay = 0;
        int yearDays = 0;
        int beforeInputMonthDay = 0; // 输入月份的前面月对应天数
        // 计算1900到输入查询年份的天数(输入年前的天数)
        for (int i = 1900; i < year; i++) {
            // 计算1900年到输入查询年份 前一年 的天数
            if (IsLeapYear(i)) {
                yearDays += 366;
            } else {
                yearDays += 365;
            }
        }
        // 计算输入查询年份对应输入月份前的天数(输入月前的天数)
        for (int j = 1; j < month; j++) {
            beforeInputMonthDay += weekDay(j, year);
        }
        // 计算输入月的天数
        int inputMonthDay = weekDay(month, year);
        sumDay = yearDays + beforeInputMonthDay;
        // 计算查询的月份1号是星期几
        int weekDay1st = sumDay % 7 + 1;
        // 打印日历
        System.out.println("一\t二\t三\t四\t五\t六\t日");
        for (int i = 1; i < weekDay1st; i++) {
            System.out.print(" \t");
        }
        for (int i = 1; i <= inputMonthDay; i++) { // 1号开始展示,所有i从1开始
            System.out.print(i + "\t");
            if ((i - 1 + weekDay1st) % 7 == 0) { // i从1开始,数组索引小标从0开始,所有这里要减1
                System.out.println();
            }
        }
    }

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

    // 判断每个月的天数(2月份需要判断是否是闰年)
    public static int weekDay(int month, int year) {
        int monthday = 0;
        switch (month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                monthday = 31;
                break;
            case 2:
                if (IsLeapYear(year))
                    monthday = 29;
                else
                    monthday = 28;
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                monthday = 30;
                break;
            default:
                System.out.print("请输入1-12月份");
                break;
        }
        return monthday;
    }
}

3、测试验证

 

若本文有帮助到阅读本文的同学,欢迎点赞、关注、收藏,互相学习交流。 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GoodTimeGGB

鼓励一下!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值