import java.util.Scanner;
public class Almanac {
public static void main(String[] args) {
// 万年历制作
Scanner input = new Scanner(System.in);
String num = "";
do {
System.out.println("*************万年历*************\n");
System.out.print("请输入年份:");
int year = input.nextInt();
System.out.print("请输入月份:");
int yuefei = input.nextInt();
if (year > 0 && yuefei > 0 && yuefei <= 12) {
int total = 0;
int sum = 0;
// 算出年份差的天数
for (int i = 1900; i < year; i++) {
if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) {
total += 366;
} else {
total += 365;
}
}
// 算出月份差的天数
for (int j = 1; j < yuefei; j++) {
switch (j) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
total += 31;
break;
case 4:
case 6:
case 9:
case 11:
total += 30;
break;
case 2:
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
total += 29;
} else {
total += 28;
}
break;
default:
System.out.println("您输入错误!");
break;
}
}
// 计算出星期几
int day = total % 7 + 1;
int days = 0;
System.out.print("\n\t\t\t" + year + "年\t\t" + yuefei + "月");
System.out.print("\n星期天\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六\n");
switch (yuefei) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days = 31;
break;
case 4:
case 6:
case 9:
case 11:
days = 30;
break;
case 2:
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
days = 29;
} else {
days = 28;
}
break;
}
for (int i = 1; i <= day; i++) {
System.out.print("\t");
sum += 1;
}
for (int i = 1; i <= days; i++) {
if (sum % 7 == 0) {
System.out.print("\n");
}
System.out.print(i + "\t");
sum += 1;
}
} else {
System.out.println("您输入错误!");
}
System.out.println("\n要继续吗?(y/n)");
num = input.next();
} while (num.equals("y"));
System.out.println("谢谢使用!");
}
} |
|