第五章第二十九题(显示日历)(Display calendars)

第五章第二十九题(显示日历)(Display calendars)

  • **5.29(显示日历)编写程序,提示用户输入年份和代表该年的第一天是星期几的数字,然后在控制台上显示该年的日历表。例如,如果用户输入年份2013和代表2013年1月1日为星期二的2,程序应该显示该年的每个月的日历,如下所示:
    January 2013
    Sun Mon Tue Wed Thu Fri Sat
    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 30 31
    . . .
    December 2013
    Sun Mon Tue Wed Thu Fri Sat
    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 30 31
    **5.29(Display calendars) Write a program that prompts the user to enter the year and first day of the year and displays the calendar table for the year on the console. For example, if the user entered the year 2013, and 2 for Tuesday, January 1, 2013, your program should display the calendar for each month in the year, as follows:
    January 2013
    Sun Mon Tue Wed Thu Fri Sat
    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 30 31
    . . .
    December 2013
    Sun Mon Tue Wed Thu Fri Sat
    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 30 31
  • 参考代码:
package chapter05;

import java.util.Scanner;

public class Code_29 {
    public static void main(String[] args) {

        int year,firstDay,totalDay = 0,febDays = 28;

        Scanner inputScanner = new Scanner(System.in);

        System.out.print("Enter the year: ");
        year = inputScanner.nextInt();

        System.out.print("Enter the first day of the year: ");
        firstDay = inputScanner.nextInt();

        for(int month = 1;month <= 12;month++)
        {
            if(month == 1)
            {
                System.out.printf("\t\t%10s%5d\n","January",year);
                System.out.println("---------------------------------------------------");
                System.out.println("Sun\tMon\tTue\tWed\tThu\tFri\tSat");
                for(int i = 1;i <= 31;i++)
                {
                    if(i == 1)
                        for(int tab = 1;tab <= firstDay;tab++)
                            System.out.print("\t");
                    System.out.printf("%3d\t",i);
                    if((i + firstDay) % 7 == 0)
                        System.out.print("\n");
                }
                System.out.print("\n");
                totalDay += 31;
            }
            else if(month == 2)
            {
                System.out.printf("\t\t%10s%5d\n","February",year);
                System.out.println("---------------------------------------------------");
                System.out.println("Sun\tMon\tTue\tWed\tThu\tFri\tSat");
                if(year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
                    febDays = 29;
                for(int i = 1;i <= febDays;i++) {
                    if(i == 1)
                        for(int tab = 1;tab <= (firstDay + totalDay) % 7;tab++)
                            System.out.print("\t");
                    System.out.printf("%3d\t",i);
                    if((i + firstDay + totalDay) % 7 == 0)
                        System.out.print("\n");
                }
                System.out.print("\n");
                totalDay += febDays;
            }
            else if(month == 3)
            {
                System.out.printf("\t\t%10s%5d\n","March",year);
                System.out.println("---------------------------------------------------");
                System.out.println("Sun\tMon\tTue\tWed\tThu\tFri\tSat");
                for(int i = 1;i <= 31;i++)
                {
                    if(i == 1)
                        for(int tab = 1;tab <= (firstDay + totalDay) % 7;tab++)
                            System.out.print("\t");
                    System.out.printf("%3d\t",i);
                    if((i + firstDay + totalDay) % 7 == 0)
                        System.out.print("\n");
                }
                System.out.print("\n");
                totalDay += 31;
            }
            else if(month == 4)
            {
                System.out.printf("\t\t%10s%5d\n","April",year);
                System.out.println("---------------------------------------------------");
                System.out.println("Sun\tMon\tTue\tWed\tThu\tFri\tSat");
                for(int i = 1;i <= 30;i++)
                {
                    if(i == 1)
                        for(int tab = 1;tab <= (firstDay + totalDay) % 7;tab++)
                            System.out.print("\t");
                    System.out.printf("%3d\t",i);
                    if((i + firstDay + totalDay) % 7 == 0)
                        System.out.print("\n");
                }
                System.out.print("\n");
                totalDay += 30;
            }
            else if(month == 5)
            {
                System.out.printf("\t\t%10s%5d\n","May",year);
                System.out.println("---------------------------------------------------");
                System.out.println("Sun\tMon\tTue\tWed\tThu\tFri\tSat");
                for(int i = 1;i <= 31;i++)
                {
                    if(i == 1)
                        for(int tab = 1;tab <= (firstDay + totalDay) % 7;tab++)
                            System.out.print("\t");
                    System.out.printf("%3d\t",i);
                    if((i + firstDay + totalDay) % 7 == 0)
                        System.out.print("\n");
                }
                System.out.print("\n");
                totalDay += 31;
            }
            else if(month == 6)
            {
                System.out.printf("\t\t%10s%5d\n","June",year);
                System.out.println("---------------------------------------------------");
                System.out.println("Sun\tMon\tTue\tWed\tThu\tFri\tSat");
                for(int i = 1;i <= 30;i++)
                {
                    if(i == 1)
                        for(int tab = 1;tab <= (firstDay + totalDay) % 7;tab++)
                            System.out.print("\t");
                    System.out.printf("%3d\t",i);
                    if((i + firstDay + totalDay) % 7 == 0)
                        System.out.print("\n");
                }
                System.out.print("\n");
                totalDay += 30;
            }
            else if(month == 7)
            {
                System.out.printf("\t\t%10s%5d\n","July",year);
                System.out.println("---------------------------------------------------");
                System.out.println("Sun\tMon\tTue\tWed\tThu\tFri\tSat");
                for(int i = 1;i <= 31;i++)
                {
                    if(i == 1)
                        for(int tab = 1;tab <= (firstDay + totalDay) % 7;tab++)
                            System.out.print("\t");
                    System.out.printf("%3d\t",i);
                    if((i + firstDay + totalDay) % 7 == 0)
                        System.out.print("\n");
                }
                System.out.print("\n");
                totalDay += 31;
            }
            else if(month == 8)
            {
                System.out.printf("\t\t%10s%5d\n","August ",year);
                System.out.println("---------------------------------------------------");
                System.out.println("Sun\tMon\tTue\tWed\tThu\tFri\tSat");
                for(int i = 1;i <= 31;i++)
                {
                    if(i == 1)
                        for(int tab = 1;tab <= (firstDay + totalDay) % 7;tab++)
                            System.out.print("\t");
                    System.out.printf("%3d\t",i);
                    if((i + firstDay + totalDay) % 7 == 0)
                        System.out.print("\n");
                }
                System.out.print("\n");
                totalDay += 31;
            }
            else if(month == 9)
            {
                System.out.printf("\t\t%10s%5d\n","September",year);
                System.out.println("---------------------------------------------------");
                System.out.println("Sun\tMon\tTue\tWed\tThu\tFri\tSat");
                for(int i = 1;i <= 30;i++)
                {
                    if(i == 1)
                        for(int tab = 1;tab <= (firstDay + totalDay) % 7;tab++)
                            System.out.print("\t");
                    System.out.printf("%3d\t",i);
                    if((i + firstDay + totalDay) % 7 == 0)
                        System.out.print("\n");
                }
                System.out.print("\n");
                totalDay += 30;
            }
            else if(month == 10)
            {
                System.out.printf("\t\t%10s%5d\n","October",year);
                System.out.println("---------------------------------------------------");
                System.out.println("Sun\tMon\tTue\tWed\tThu\tFri\tSat");
                for(int i = 1;i <= 31;i++)
                {
                    if(i == 1)
                        for(int tab = 1;tab <= (firstDay + totalDay) % 7;tab++)
                            System.out.print("\t");
                    System.out.printf("%3d\t",i);
                    if((i + firstDay + totalDay) % 7 == 0)
                        System.out.print("\n");
                }
                System.out.print("\n");
                totalDay += 31;
            }
            else if(month == 11)
            {
                System.out.printf("\t\t%10s%5d\n","November",year);
                System.out.println("---------------------------------------------------");
                System.out.println("Sun\tMon\tTue\tWed\tThu\tFri\tSat");
                for(int i = 1;i <= 30;i++)
                {
                    if(i == 1)
                        for(int tab = 1;tab <= (firstDay + totalDay) % 7;tab++)
                            System.out.print("\t");
                    System.out.printf("%3d\t",i);
                    if((i + firstDay + totalDay) % 7 == 0)
                        System.out.print("\n");
                }
                System.out.print("\n");
                totalDay += 30;
            }
            else if(month == 12)
            {
                System.out.printf("\t\t%10s%5d\n","December",year);
                System.out.println("---------------------------------------------------");
                System.out.println("Sun\tMon\tTue\tWed\tThu\tFri\tSat");
                for(int i = 1;i <= 31;i++)
                {
                    if(i == 1)
                        for(int tab = 1;tab <= (firstDay + totalDay) % 7;tab++)
                            System.out.print("\t");
                    System.out.printf("%3d\t",i);
                    if((i + firstDay + totalDay) % 7 == 0)
                        System.out.print("\n");
                }
                System.out.print("\n");
            }
        }
    }
}

  • 结果显示:
Enter the year: 2013
Enter the first day of the year: 2
		   January 2013
---------------------------------------------------
Sun	Mon	Tue	Wed	Thu	Fri	Sat
		  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	 30	 31	
		  February 2013
---------------------------------------------------
Sun	Mon	Tue	Wed	Thu	Fri	Sat
					  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	
		     March 2013
---------------------------------------------------
Sun	Mon	Tue	Wed	Thu	Fri	Sat
					  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	 30	
 31	
		     April 2013
---------------------------------------------------
Sun	Mon	Tue	Wed	Thu	Fri	Sat
	  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	 30	
		       May 2013
---------------------------------------------------
Sun	Mon	Tue	Wed	Thu	Fri	Sat
			  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	 30	 31	
		      June 2013
---------------------------------------------------
Sun	Mon	Tue	Wed	Thu	Fri	Sat
						  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	
 30	
		      July 2013
---------------------------------------------------
Sun	Mon	Tue	Wed	Thu	Fri	Sat
	  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	 30	 31	
		   August  2013
---------------------------------------------------
Sun	Mon	Tue	Wed	Thu	Fri	Sat
				  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	 30	 31	

		 September 2013
---------------------------------------------------
Sun	Mon	Tue	Wed	Thu	Fri	Sat
  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	 30	
		   October 2013
---------------------------------------------------
Sun	Mon	Tue	Wed	Thu	Fri	Sat
		  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	 30	 31	
		  November 2013
---------------------------------------------------
Sun	Mon	Tue	Wed	Thu	Fri	Sat
					  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	 30	

		  December 2013
---------------------------------------------------
Sun	Mon	Tue	Wed	Thu	Fri	Sat
  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	 30	 31	

Process finished with exit code 0

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值