public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner = new Scanner(System.in);
System.out.println("请依次输入年月信息:");
int year = scanner.nextInt();
int month = scanner.nextInt();
int sum = 0, day = 0;
// 计算从1990年开始到当前年的总天数
for (int i = 1990; i < year; i++) {
if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) {
sum += 366;
} else {
sum += 365;
}
}
// 计算当年内到当前月的天数
for (int j = 1; j <= month; j++) {
switch (j) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
sum += 31;
day = 31;
break;
case 4:
case 6:
case 9:
case 11:
sum += 30;
day = 30;
break;
case 2:
if (j % 4 == 0 && j % 100 != 0 || j % 400 == 0) {
sum += 29;
day = 29;
} else {
sum += 28;
day = 28;
}
break;
}
//System.out.println(day);
}
//System.out.println(sum);
System.out.println("日\t一\t二\t三\t四\t五\t六");
// 计算出上个月多余的天数并跳过
int moreDay = (sum - day) % 7 + 1;
for (int i = 1; i <= moreDay; i++) {
System.out.print("\t");
}
for (int i = 1; i <= day; i++) {
if ((sum-day)%7+1==6) {
System.out.print(i + "\n");
}
else {
System.out.print(i + "\t");
}
sum += 1;
}
}
输入年月信息,打印对应月的日历
最新推荐文章于 2022-12-22 18:15:46 发布