万年历的Java实现
导入Java . util . *;
李万年公共课{
公共静态void main(String args【】){
扫描仪输入=新扫描仪(system . in);
System.out.print(“请输入年份:“);
int year = input . nextint();
System.out.print(“请输入月份:“);
int month = input . nextint();
int totalDays=1,month days = 0;
//计算总天数
for(int I = 1900;我& lt年份;i++){
if((I % 4 = = 0 & amp;& amp我%100!= 0)| |(I % 400 = = 0){
总天数=总天数+366;
}否则{
总天数=总天数+365;
}
}
for(int passed month = 1;passedMonth & lt=月;passed month++ ){
if(passed month = = 1 | | passed month = = 3 | | passed month = = 5 | | passed month = = 7 | | passed month = = 8 | | passed month = = 10 | | passed month = = 12 ){
month days = 31;
} else if(passed month = = 2 ){
if((year % 4 = = 0 & amp;& amp年份%100!= 0)| |(year % 400 = = 0){
月数= 29;
}否则{
monthDays = 28
}
}否则{
monthDays = 30
}
if(passed month & lt;月){
总天数=总天数+月天数;
}
}////总天数计算结束
//打印格式,对齐
System.out.println(“星期日\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六“);
int temp =(总天数% 7);
for(int p = 0;p & lttempp++){
system . out . print(“\ t“);
}
for(int w = 1;w & lt=月天数;w++ ){
system . out . print(w+“\ t“);
if((总天数+w)% 7 = = 0 ){
system . out . print(“\ n“);
}
}
}
}