打印万年历

1.从键盘输入年份月份

Scanner input= new Scanner(System.in);
        System.out.println("请输入年份: ");
        int year = input.nextInt();//键盘输入年份
        Scanner inpt= new Scanner(System.in);
        System.out.println("请输入月份: ");
        int month = input.nextInt();//键盘输入月份

2.确定整年天数

确定从1900年至2021年的天数

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

        }

3.确定整月天数

int zhenYue = 0;
        for (int x = 1; x < month; x++) {//取不到month当月
            switch (x) {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    zhenYue = zhenYue + 31;
                    break;
                case 4:
                case 6:
                case 9:
                case 11:
                    zhenYue = zhenYue + 30;
                    break;
            }
        }

4.确定某月的第一天是星期几

利用公式:星期几 =(某年某月某日至1900年1月1日的天数-1)%7+1

 int XinQiJi;
        XinQiJi=(zhenNian+zhenYue+1-1)%7+1;//确定某月的第一天是星期几

5.确定当月天数(31、30、29、28)

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

6.打印日历头

System.out.println("一\t二\t三\t四\t五\t六\t日");

7.补齐空格日期

int ShangYueT=0;
        int ymothon=month-1;
        if (ymothon==0){
            ymothon=12;
        }
        switch (ymothon) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                ShangYueT = 31;
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                ShangYueT = 30;
                break;
            default:
                if (year % 400 == 0 || year % 4 == 0 && year % 100 != 0) {
                    ShangYueT=29;
                } else {
                    ShangYueT=28;
                }
        }
        //确定上月天数结束
        for (int w =ShangYueT-XinQiJi+2 ; w <= ShangYueT ; w++) {
            System.out.print(w+"\t");
        }

8.打印当月每一天,遇到星期日换行

for (int y = 1; y <=DangYueT ; y++) {
            System.out.print(y+"\t");
            int huanHang=(zhenNian+zhenYue+y-1)%7+1;
            if (huanHang==7) {
                System.out.println();//到星期日换行
            }
        }

9.补齐后面空格的日期

 //1.确定还剩多少格子
        int ShenYuGe=42-DangYueT-(XinQiJi-1);
        for (int c = 1; c <=ShenYuGe ; c++) {
            System.out.print(c+"\t");
            int zaihuanHang=(zhenNian+zhenYue+DangYueT+c-1)%7+1;
            if (zaihuanHang==7) {
                System.out.println();//到星期日换行
            }
        }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值