import java.util.Scanner;
//1900年1月1日是星期一
public class Calender {
public static void main(String[] args) {
System.out.println("请输入年份:");
Scanner input=new Scanner(System.in);
int year=input.nextInt();
System.out.println("请输入月份");
int month=input.nextInt();
int total=calcYear(year);
for(int i=0;i<month;i++){
total=total+calcMonth(i,year);
}
int week=(total+1)%7;
System.out.println("星期日\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六");
for(int i=0;i<week;i++){
System.out.print("\t");
}
int monthDay=0;
monthDay=calcMonth(month,year);
for(int i=1;i<=monthDay;i++){
System.out.print(" "+i+"\t");
if((week+i)%7==0){
System.out.println();
}
}
}
public static int calcYear(int year){
int yearTotal = 0;
for(int i = 1900;i<year;i++){
if(runnian(i)){
yearTotal = yearTotal + 366;
}else{
yearTotal = yearTotal + 365;
}
}
return yearTotal;
}
public static boolean runnian(int year){
boolean bool=false;
if((year%4==0 &&year%100!=0) || (year%400==0)){
bool=true;
}
return bool;
}
public static int calcMonth(int month,int year){
int monthDay=0;
switch(month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
monthDay=31;
break;
case 4:
case 6:
case 9:
case 11:
monthDay=30;
break;
case 2:
if(runnian(year)){
monthDay=29;
}else{
monthDay=28;
}
break;
}
return monthDay;
}
}
根据年份和月份打印当月日历
最新推荐文章于 2021-06-25 13:49:15 发布