输入指定的年份与月份,看这个月的日历表
package Xueying_Liu;
import java.util.Scanner;
public class rilibiao {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入您选择的年份");
int year = sc.nextInt();
System.out.println("请输入您选择的月份");
int month = sc.nextInt();
//记录一共有多少天
int count = 1;
//从1990年到输入的这一年之前一共有多少天
for (int i = 1990; i < year; i++) {
if ((i % 4 == 0 && i % 100 != 0) || i % 400 == 0) {
count += 366;
} else {
count += 365;
}
}
//看输入的那一年是不是闰年
boolean bool = false;
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
bool = true;
}
//看输入的那一年在这个月之前有多少天
for (int i = 1; i < month; i++) {
switch (i) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
count += 31;
break;
case 2:
if (bool)
count += 29;
else
count += 28;
break;
default:
count += 30;
}
}
//记录那个月有多少天
int day = 0;
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
day = 31;
break;
case 2:
if (bool)
day = 29;
else
day = 28;
break;
default:
day = 30;
}
// System.out.println(count);
System.out.println("周日 周一 周二 周三 周四 周五 周六 ");
//week记录是周几,周日为0
int week = count % 7;
//这个月第一天可能不是周日,需要输出空格
for (int i = 0; i < week; i++) {
System.out.print(" ");
}
for (int i = 1; i <= day; i++) {
//每加一天,week加一,当这一周满了以后,就会换行
if (week == 7) {
System.out.println();
week = 0;
}
week++;
System.out.print(i + " ");
}
}
}