Java实现日历打印

题目要求:
接收一个年份数(大于等于1900)和一个月份数,打印出该月的日历。
日历输出格式如下:

日 一 二 三 四 五 六
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29

其中日期上下的分隔符用的是50个=。
日期之间以及星期之间使用/t分隔。
1900年1月1日是星期1。

下面来看一下简单的思路分析吧!!!
思路分析:
求1900年到输入年份之间的累计天数,其中闰年366天,平年365天;
求1月到输入月份之间的累计天数;
得到1900-1-1到输入年月之前所有天数,用总天数对7求余,对余数加1,该数值即为该月1号的星期;
判断输入月份有多少天;
控制格式打印日历。
*/
源程序:


import java.util.Scanner;
public class Calendar{
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int year = in.nextInt();
        int month = in.nextInt();
        if(year<1900||(month>12||month<1)){
            if(year<1900){
                System.out.print("请输入大于或等于1900的年份") ;
            }
            if(month>12||month<1){
                System.out.print("请输入正确的月份") ;
            }
        }else{
            System.out.println("==================================================");
            int sum = 0;
            int k = 0;
            for (int i = 1; i <= month - 1; i++) {
                if (i == 2) {
                    if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
                        sum = sum + 29;
                    } else {
                        sum = sum + 28;
                    }
                } else {
                    if (i == 4 || i == 6 || i == 9 || i == 11) {
                        sum = sum + 30;
                    } else {
                        sum = sum + 31;
                    }
                }
            }
            for (int a = 1900; a <= year - 1; a++) {
                if (a % 4 == 0 && a % 100 != 0 || a % 400 == 0) {
                    sum = sum + 366;
                } else {
                    sum += 365;
                }
            }
            sum += 1;
            int wekday = sum % 7;
            System.out.println("日\t一\t二\t三\t四\t五\t六");
            for (int j = 1; j <= wekday; j++) {
                System.out.print("\t");
            }
            if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
                if (month == 2) {
                    k = 29;
                } else if (month == 4 || month == 6 || month == 9 || month == 11) {
                    k = 30;
                } else {
                    k = 31;
                }
            } else {
                if (month == 2) {
                    k = 28;
                } else if (month == 4 || month == 6 || month == 9 || month == 11) {
                    k = 30;
                } else {
                    k = 31;
                }
            }
            for (int i = 1; i <= k; i++) {
                if (sum % 7 == 6) {
                    System.out.print(i + "\n");
                } else {
                    System.out.print(i + "\t");
                }
                sum++;
            }
            System.out.println("\n");
            System.out.println("==================================================");
        }
    }

}


实现效果:
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值