Java语言程序设计第5章编程练习题5.29(显示日历)提示用户输入年份和代表该年第一天是星期几的数字,然后在控制台上显示该年的日历表。

import java.util.Scanner;

public class Show_calendar {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the year and the first day: ");
        int year = input.nextInt(); // 年份
        int day = input.nextInt(); // 当年1月1号是星期几
        int feb_days = (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) ? 29 : 28; // 计算二月份的天数
        int month, date, num_blank, days = 0; // 月份,日期,当月第一天前面的空格数,当月的天数
        for (month = 1; month <= 12; month++) {
            day += days;
            days = 31;
            switch (month) {
                case 1: System.out.print("\t\tJanuary "); break;
                case 2: System.out.print("\t\tFebruary "); days = feb_days; break;
                case 3: System.out.print("\t\tMarch "); break;
                case 4: System.out.print("\t\tApril "); days = 30; break;
                case 5: System.out.print("\t\tMay "); break;
                case 6: System.out.print("\t\tJune "); days = 30; break;
                case 7: System.out.print("\t\tJuly "); break;
                case 8: System.out.print("\t\tAugust "); break;
                case 9: System.out.print("\t\tSeptember "); days = 30; break;
                case 10: System.out.print("\t\tOctober "); break;
                case 11: System.out.print("\t\tNovember "); days = 30; break;
                case 12: System.out.print("\t\tDecember ");
            }
            System.out.println(year);
            System.out.println("----------------------------");
            System.out.println("Sun\tMon\tTue\tWed\tThu\tFri\tSat");
            for (num_blank = 0; num_blank < day % 7; num_blank++)
                System.out.print(" \t");
            for (date = 1; date <= days; date++)
                if ((date + num_blank) % 7 == 0)
                    System.out.println(date);
                else
                    System.out.print(date + "\t");
            if ((date - 1 + num_blank) % 7 != 0)
                System.out.println();
        }
    }
}
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

秋凉78

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

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

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

打赏作者

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

抵扣说明:

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

余额充值