万年历(执行语句版)
需求:输入年和月,打印日历。
import java.util.Scanner;
public class Test{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
System.out.println("请输入查询的年份:");
int year = scan.nextInt();
System.out.println("请输入查询的月份:");
int month = scan.nextInt();
int allDayOfYear = 0;
int allDayOfMonth = 0;
for(int a=1900;a<year;a++){
if(a%4==0 && a%100!=0 || a%400==0){
allDayOfYear += 366 ;
}else{
allDayOfYear += 365 ;
}
}
for(int a=1; a<month;a++){
switch(a){
case 1 : case 3 : case 5 : case 7 : case 8 : case 10 : case 12 :
allDayOfMonth += 31;
break;
case 4 : case 6 : case 9 : case 11 :
allDayOfMonth += 30;
break;
case 2:
if(year%4==0 && year%100!=0 || year%400==0){
allDayOfMonth +=29;
}else{
allDayOfMonth +=28;
}
break;
}
}
int allDay=allDayOfYear+allDayOfMonth+1;
int week =allDay%7;
if(week ==0){
week = 7;
}
int day=0;
switch(month){
case 1 : case 3 : case 5 : case 7 : case 8 : case 10 : case 12 :
day= 31;
break;
case 4 : case 6 : case 9 : case 11 :
day= 30;
break;
case 2:
if(year%4==0 && year%100!=0 || year%400==0){
day=29;
}else{
day=28;
}
break;
}
if(year<1900 || month>12 || month <1){
System.out.println("\n输入的查询日期有误!");
}else{
System.out.println(year+"年"+month+"月");
System.out.println(month + "月,有"+day+"天");
System.out.println("一\t二\t三\t四\t五\t六\t日");
int count=0;
for(int a=1;a<week;a++){
System.out.print("\t");
count++;
}
for(int a =1;a<=day;a++){
System.out.print(a+"\t");
count++;
if(count%7==0){
System.out.print("\n");
}
}
}
}
}
运行结果: