import java.util.Scanner;
public class calendar{
public static void main(String[] args){
int year =0 ;
int month = 0;
int monthday = 0;
int sum = 0 ;//定义总天数
System.out.println("输入年:");
Scanner input1 = new Scanner(System.in);
year = input1.nextInt();
System.out.println("输入月:");
Scanner input2 = new Scanner(System.in);
month = input2.nextInt();
//判断是否为闰年
boolean isRun = true;
if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0){
isRun = true ;
}else{
isRun = false;
}
//计算距离1900年1月1日的天数
for(int i = 1900;i <year;i++){
if(isRun){
sum += 366;
}else{
sum += 365 ;
}
}
for(int i = 1;i <= month;i++){
switch(i){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
monthday = 31;
case 4:
case 6:
case 9:
case 11:
monthday = 30;
case 2:
if(isRun){
monthday = 29 ;
}else{
monthday = 28 ;
}break;
default:System.out.println("您输入的信息有误!");
}
}
sum = sum + monthday ;
System.out.println("日\t一\t二\t三\t四\t五\t六");
sum = sum+1-monthday;
//除以7便是从那天开始
int length = sum%7;
//之前用空格 打印
for (int i = 0; i <= length-1; i++) {
System.out.print(" \t");
}
for(int i = 1;i <=monthday;i++){
if (sum%7==6) {
System.out.print(i + "\n");
}else{
System.out.print(i + "\t");
}
sum++;
}
}
}
打印结果: