0
点赞
收藏
分享

微信扫一扫

第五章第二十九题(显示日历)(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


 


 

下面是参考答案代码:

import java.util.Scanner;

public class DisplayCalendarsQuestion29 {
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");
}
}

inputScanner.close();
}
}

运行效果:

第五章第二十九题(显示日历)(Display calendars)_代码规范

 

注:编写程序要养成良好习惯
1.文件名要用英文,具体一点
2.注释要英文
3.变量命名要具体,不要抽象(如:a,b,c等等),形式要驼峰化
4.整体书写风格要统一(不要这里是驼峰,那里是下划线,这里的逻辑段落空三行,那里相同的逻辑段落空5行等等)
5.普通变量,方法名要小驼峰,类名要大驼峰,常量要使用全部大写加上下划线命名法
6.要学习相应的代码编辑器的一些常用快捷键,如:快速对齐等等

举报

相关推荐

0 条评论