打印万年历
要求:
(1)编写一个方法判断闰年;
(2)编写一个方法判断某年某月有多少天;
(3)编写一个方法计算某年某月前距离1900年1月1日的总天数;
(4)编写一个输出某年某月日历的方法;
(5)编写一个测试方法。(输入年份,月份,输出该月的日历,已知1900年1月1日是星期一),
代码:
import java.util.Scanner;
public class aaaa {
public static void main(String[]args){
Scanner input = new Scanner(System.in );
System.out.print("请输入年份:");
int years = input.nextInt();
if(years<1900){System.out.print("年份输入不正确!拜拜!");return ;}
System.out.print("请输入月份:");
int month = input.nextInt();
if(month<1|month>12){System.out.print("月份输入不正确!拜拜!");return ;}
if (run(years)==1)
{System.out.println(+years+"年是闰年!");}
if (run(years)==0)
{System.out.println(+years+"年不是闰年!");}
zongtianshu(years,month);
System.out.println(+years+"年"+month+"月有"+monthdays( month, years )+"天!");
System.out.println("距离1900年1月1日"+zongtianshu( years , month)+"天");
rili(years,month);
}
public static int run(int years){ //是否闰年
int run = 0;
if((years % 4 == 0 && years % 100 != 0) || (years % 400 == 0)){
run = 1;}
return run;
}
public static int monthdays(int month,int years ){ //某年某月的月天数
int days=0;
for(int j = 1; j <= month; j++){
switch(j)
{case 1:case 3:case 5:case 7:case 8:case 10:case 12:
days = 31;break;
case 4:case 6:case 9:case 11:
days = 30;break;
case 2:
if(run(years)==1) {days = 29;}
else {days = 28;}
}
}return days;
}
public static int zongtianshu(int years ,int month){
int days = 0;
int beforeDays = 0;
int totalDays = 0; //累计天数
//距离1900年1月1日的天数
for(int i = 1900; i < years; i++){
if((i % 4 == 0 && i % 100 != 0) || (i % 400 == 0))
{totalDays = totalDays + 366;}
else{totalDays = totalDays + 365;}}
for(int j = 1; j <= month; j++){
switch(j)
{case 1:case 3:case 5:case 7:case 8:case 10:case 12:
days = 31;break;
case 4:case 6:case 9:case 11:
days = 30;break;
case 2:
if(run(years)==1) {days = 29;}
else {days = 28;}
}
if(j < month){beforeDays = beforeDays + days;}
}
totalDays = totalDays + beforeDays; //总天数
return totalDays;
}
public static int rili(int years,int month){
int diyitian = 0;
int rrr = 1 + zongtianshu(years,month) % 7 ;
if(rrr == 7){
diyitian = 0; //周日
}
else{
diyitian = rrr;
}
//输出日历
System.out.println(+years+"年"+month+"月 日历表:");
System.out.println("星期日\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六");
for(int k = 0; k < diyitian; k++)
{System.out.print("\t");}
for(int m = 1; m <= monthdays(month,years); m++){
System.out.print( m + "\t");
if((zongtianshu(years,month) + m +1) % 7 == 0){
System.out.print("\n");
}
}
return 0;
}
}

浙公网安备 33010602011771号