50,526
社区成员
发帖
与我相关
我的任务
分享
public class JudgeWeekday {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
// Prompt the user to enter input
System.out.print("请输入一个年份: ");
int year = input.nextInt();
System.out.print("请输入该年以星期几开始: ");
int firstDay = input.nextInt();
int startDay = firstDay;
int numberOfDaysInMonth = 0;
// Display calendar for each month
for (int month = 1; month <= 12; month++) {
// Display Calendar title
System.out.print(" ");
switch (month) {
case 1:
System.out.println("一月 " + year);
numberOfDaysInMonth = 31;
break;
case 2:
System.out.println("二月 " + year);
if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
numberOfDaysInMonth = 29;
else
numberOfDaysInMonth = 28;
break;
case 3:
System.out.println("三月 " + year);
numberOfDaysInMonth = 31;
break;
case 4:
System.out.println("四月 " + year);
numberOfDaysInMonth = 30;
break;
case 5:
System.out.println("五月 " + year);
numberOfDaysInMonth = 31;
break;
case 6:
System.out.println("六月 " + year);
numberOfDaysInMonth = 30;
break;
case 7:
System.out.println("七月 " + year);
numberOfDaysInMonth = 31;
break;
case 8:
System.out.println("八月 " + year);
numberOfDaysInMonth = 31;
break;
case 9:
System.out.println("九月 " + year);
numberOfDaysInMonth = 30;
break;
case 10:
System.out.println("十月 " + year);
numberOfDaysInMonth = 31;
break;
case 11:
System.out.println("十一月 " + year);
numberOfDaysInMonth = 30;
break;
case 12:
System.out.println("十二月 " + year);
numberOfDaysInMonth = 31;
break;
}
System.out.println("-----------------------------");
System.out.println(" 日 一 二 三 四 五 六");
// Pad space before the first day of the month
int i = 0;
for (i = 0; i < startDay; i++)
System.out.print(" ");
for (i = 1; i <= numberOfDaysInMonth; i++) {
if (i < 10)
System.out.print(" " + i);
else
System.out.print(" " + i);
if ((i + startDay) % 7 == 0)
System.out.println();
}
System.out.println();
System.out.println();
// Get the start day for the next month
startDay = (startDay + numberOfDaysInMonth) % 7;
}
}
}
效果: