万年历(java)

本文详细介绍了如何使用Java编程语言,通过Scanner类获取用户输入的年份和月份,然后计算并打印出该月的日历,包括处理闰年的逻辑和一周的天数分布。
摘要由CSDN通过智能技术生成

1.根据输入的年份和月份,打印出当月的日历

import java.util.Scanner;

public class Demo13 {
    public static void main(String[] args) {
        System.out.println("输入年份");
        Scanner w=new Scanner(System.in);
        int year=w.nextInt();
        System.out.println("输入月份");
        int month=w.nextInt();
        System.out.println(year+"年份"+month+"月份");


        int monthDays;
        if(month==1||month==3||month==5||month==7||month==8||month==10||month==12){
            monthDays=31;
        }else if(month==4||month==6||month==9||month==11){
            monthDays=30;
        }else{
            if((year%4==0&&year%100!=0)||year%400==0){
                monthDays=29;
            }else{
                monthDays=28;
            }
        }


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

            } else {
                sumYear += 365;
            }
        }

        int sumMonth=0;
        for(int i=1;i<month;i++){
            if(i==1||i==3||i==5||i==7||i==8||i==10||i==12){
                sumMonth+=31;
            }else if(i==4||i==6||i==9||i==11){
                sumMonth+=30;
            }else {
                    if(year%4==0&&year%100!=0||year%400==0){
                        sumMonth+=29;
                    } else {
                        sumMonth += 28;
                    }
                }
            }
        int week=(sumMonth+sumYear+1)%7;
        System.out.println("一\t二\t三\t四\t五\t六\t日");
        for(int i=1;i<=week-1;i++) {
            System.out.print("\t");
        }
        for(int i=1;i<=monthDays;i++){
            System.out.print(i+"\t");
            if((sumMonth+sumYear+i)%7==0){
                System.out.println();
            }
        }

    }


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值