编写程序:已知1901年1月1日为星期二,请编写一个程序,提示用户输入任意年份的数字(包括小于1901年),显示该年份1-12月的完整日历。
输入格式:
int year = scanner.nextInt();
int month = scanner.nextInt();
输出格式:
输出月头代码:
System.out.println(" " + getMonthName(month) + " " + year);
System.out.println("-----------------------------");
System.out.println(" Sun Mon Tue Wed Thu Fri Sat");
输出月体代码:
if (i < 10)
System.out.print(" " + i);
else
System.out.print(" " + i);
if ((i + startDay) % 7 == 0)
System.out.println();
输入样例:
2019
1
输出样例:
January 2019
-----------------------------
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
输入样例2:
1890
1
输出样例:
January 1890
-----------------------------
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 Main {
public static void main(String[] args) {
Scanner scanner = (1) ;
int year = scanner.nextInt();
int month = scanner.nextInt();
printMonth(year, month);//调用方法打印日历
}
//打印日历
(2) printMonth( (3) ) {
printMonthTitle(year, month);//打印月头:year, month
int startDay = getStartDay(year, month);//获得该月第一天是星期几
int numOfDaysInMonth = getNumOfDaysInMonth(year, month);//获得该月有多少天
printMonthBody(startDay, numOfDaysInMonth);//需要根据第一天是星期几,打印当月天数
}
//获得该月第一天是星期几
(4) getStartDay( (5) ) {
int startDay1901 = 2;//已知1901年1月1日为星期二
if (year >= 1901) {
long totalNumOfDays = getTotalNumOfDays(year, month); //获得从1901年1月1日到指定某年某月1日的天数
return (int) ( (6) );
} else {
long totalNumOfDays = getTotalNumOfDaysBefore1901(year, month);//获得从1901年前的某年某月1日到1901年1月1日天数
if (totalNumOfDays % 7 <= 2) {
return (int) ( (7) );
} else {
return (int) ( (8) );
}
}
}
//获得从1901年1月1日到指定某年某月1日的天数
(9) getTotalNumOfDays( (10) ) {
long total = 0;
for (int i = 1901; i < year; i++)
if (isLeapYear(i))//判断是否为闰年
total = (11) ;
else
total = (12) ;
for (int i = 1; i < month; i++)
total = total + getNumOfDaysInMonth(year, i);//加上指定从1月到 month-1月的天数
return total;
}
//获得从1901年前的某年某月1日到1901年1月1日天数
(13) getTotalNumOfDaysBefore1901( (14) ) {
long total = 0;
for (int i = year + 1; i < 1901; i++)
if (isLeapYear(i)) //判断是否为闰年
total = (15) ;
else
total = (16) ;
for (int i = month; i <= 12; i++)
total = total + getNumOfDaysInMonth(year, i);//加上指定从month月到12月31的天数
return total;
}
//获得某年某月的天数
(17) getNumOfDaysInMonth( (18) ) {
if ( (19) )
return 31;
if ( (20) )
return 30;
if ( (21) )
if (isLeapYear(year))
return 29;
else
return 28;
return 0;// 如果月份不正确,返回0
}
//判断是否为闰年,是,返回true
(22) boolean isLeapYear( (23) ) {
if ( (24) )
return true;
return false;
}
//打印月头
(25) printMonthTitle( (26) ) {
System.out.println(" " + getMonthName(month) + " " + year);
System.out.println("-----------------------------");
System.out.println(" Sun Mon Tue Wed Thu Fri Sat");
}
//获取月的英文全称
(27) getMonthName( (28) ) {
String monthName = null;
switch (month) {
case 1:
monthName = "January";
break;
case 2:
monthName = "February";
break;
case 3:
monthName = "March";
break;
case 4:
monthName = "April";
break;
case 5:
monthName = "May";
break;
case 6:
monthName = "June";
break;
case 7:
monthName = "July";
break;
case 8:
monthName = "August";
break;
case 9:
monthName = "September";
break;
case 10:
monthName = "October";
break;
case 11:
monthName = "November";
break;
case 12:
monthName = "December";
}
return monthName;
}
//打印月体
(29) printMonthBody( (30) ) {
for (int i = 0; i < startDay; i++)
System.out.print(" ");
for (int i = 1; i <= numOfDaysInMonth; i++) {
if (i < 10)
System.out.print(" " + i);
else
System.out.print(" " + i);
if ((i + startDay) % 7 == 0)
System.out.println();
}
System.out.println();
}
}