import java.util.Scanner;
//万年历
public class WNL {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("请输入年份:");
int year = s.nextInt();
System.out.println("请输入月份:");
int month = s.nextInt();
int monthDays = getMonthDays(month, year);
int sumYear = 0;
for (int i = 1; i < year; i++) {
sumYear += judgeFlatLeapYear(i) ? 366 : 365;
}
int sumMonth = 0;
for (int i = 1; i < month; i++) {
sumMonth += getMonthDays(i, year);
}
int week = (sumYear + sumMonth + 1) % 7 == 0 ? 7 : (sumYear + sumMonth + 1) % 7;
System.out.println("一\t二\t三\t四\t五\t六\t日");
for (int i = 1; i < week; i++) {
System.out.print("\t");
}
for (int i = 1; i <= monthDays; i++) {
System.out.print(i + "\t");
if ((sumMonth + sumYear + i) % 7 == 0) {
System.out.println();
}
}
}
public static boolean judgeFlatLeapYear(int year) {
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
return true;
} else {
return false;
}
}
public static int getMonthDays(int month, int year) {
int monthDays = 0;
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
monthDays = 31;
break;
case 4:
case 6:
case 9:
case 11:
monthDays = 30;
break;
case 2:
monthDays = judgeFlatLeapYear(year) ? 29 : 28;
}
return monthDays;
}
}
JAVA(代码)-万年历
最新推荐文章于 2024-03-04 21:58:57 发布