求日历表(已知年份、月份)

求日历表(已知年份、月份)

1.核心代码

1.1 求前一年的最后一天距离1900年的天数sum_year

int sum_year=0;
for (int i = 1900; i <year; i++) {
     sum_year+=365;
     if(i%400==0||(i%4==0&&i%100!=0)){
        sum_year++;
     }
}

1.2 计算当前年的1月距离已知月份之间的天数sum_month

int sum_month=0;
for (int j = 1; j <month; j++) {
   switch(j){
             case 2:sum_month+=28;
                    if(year%400==0||(year%4==0&&year%100!=0)){
                        sum_month++;
                    };
                    break;
             case 4:
             case 6:
             case 9:
             case 11: sum_month+=30;
                      break;
             default: sum_month+=31;
                      break;
             }
}

1.3 打印当月1号的位置

int first_day=totaL_day%7;
if(first_day==0){
   for (int i = 0; i <6; i++) {
        System.out.print("\t");
   }
   System.out.println(1);
}else{
   for (int i = 0; i <first_day-1; i++) {
         System.out.print("\t");
   }
   System.out.print(1+"\t")
}

2.常见问题点分析

2.1 如何判断某月的某天是星期几???

解决方案:计算该天距离1900年1月1号(星期一)的天数,然后%7

​ 若结果为0,则代表当天为星期日

​ 结果为1,则代表当天为星期一

​ 结果为2,则代表当天为星期二

​ 结果为3,则代表当天为星期三

​ 结果为4,则代表当天为星期四

​ 结果为5,则代表当天为星期五

​ 结果为6,则代表当天为星期六

2.2 如何打印输出所求月份的一号

​ 解决方案:设1号距离1900年1月1号的天数为day01,

​ 若day01%7==0,则需要先打印个\t,然后打印数字1并换行

​ 若day%01%7!=0,则需要打印day01%7-1个\t,然后打印数字1和\t

2.3 如何判断当前所打印的日期后是否要换行

​ 解决方案: 设当前日期距离1900年1月1号的天数的为day02

​ 若day02%7==0,则需要打印完该日期后换行

​ 否则,则无需换行

3.运行截图

在这里插入图片描述

4.源代码

import java.util.Scanner;
public class Calendar {
    public static void main(String[] args) {
        System.out.println("求日历表(已知年份、月份)");
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入年份");
        int year=scanner.nextInt();
        while(true){
            if(year<1900){
                System.out.println("输入的年份不合条件,请重新输入");
                year=scanner.nextInt();
            }else{
                break;
            }
        }
        System.out.println("请输入月份");
        int month=scanner.nextInt();
        while(true){
            if(month<1||month>12){
                System.out.println("输入的月份不合条件,请重新输入");
                month=scanner.nextInt();
            }else{
                break;
            }
        }
        //已知1900年1月1日是星期一
        //计算当前年的前一年的最后一天距离1900年的天数sum_year,其中闰年为366天,平年为365天
        int sum_year=0;
        for (int i = 1900; i <year; i++) {
            sum_year+=365;
            if(i%400==0||(i%4==0&&i%100!=0)){
                sum_year++;
            }
        }
        //计算当前年的1月距离已知月份之间的天数sum_month
        int sum_month=0;
        //计算每月的天数
        for (int j = 1; j <month; j++) {
            switch(j){
                case 2:sum_month+=28;
                    if(year%400==0||(year%4==0&&year%100!=0)){
                        sum_month++;
                    };
                    break;
                case 4:
                case 6:
                case 9:
                case 11: sum_month+=30;
                    break;
                default: sum_month+=31;
                    break;
            }
        }
        //计算1900年1月1号距离当前月1号的天数total_day
        int totaL_day=sum_year+sum_month+1;
        System.out.println(sum_month);
        //先打印默认的格式
        System.out.println("日历");
        System.out.println(year+"年"+month+"月");
        System.out.println("一\t二\t三\t四\t五\t六\t日");
        //计算当月1号是星期几,0代表星期日,1代表星期一,2代表星期二,3代表星期三....
        int first_day=totaL_day%7;
        if(first_day==0){
            for (int i = 0; i <6; i++) {
                System.out.print("\t");
            }
            System.out.println(1);
        }else{

            System.out.print(1+"\t");
        }
        //判断今年是不是闰年
        boolean isLeap=false;
        if(year%400==0||(year%4==0&&year%100!=0)){
            isLeap=true;
        }
        //计算当前月的天数
        int current=0;
        switch (month){
            case 2: current+=28;
                    if(isLeap) {
                        current++;
                    }
                    break;
            case 4:
            case 6:
            case 9:
            case 11:current+=30;
                    break;
            default:current+=31;
                    break;
        }
        //打印1号后的日历表
        for (int k = 2; k <=current; k++) {
            int temp=first_day-1+k;
            if(temp%7==0){
                System.out.println(k);
                continue;
            }
            System.out.print(k+"\t");
        }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

SSS4362

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值