java培训-----第五天

时间:2021-07-30 20:45:16

最后一天,写个万年历,不会打印出来,借鉴下同学的java培训-----第五天

package figure_day4;


import java.util.Scanner;


public class test01 {
public final static int YEAR4 = 365 * 3 + 366;
public final static int YEAR = 365;


public static void main(String[] args) {


int year, month, day;// user input


int yearDay = 0, monthDay = 0;// present data


int wholeYearDay = 0, wholeMonthDay = 0, wholeDayDay = 0;// distance
// data


int k = 0;// register


int week = 0;


System.out.println("请输入年份:");
System.out.println("(1900--11900年)");

Scanner input = new Scanner(System.in);
year = input.nextInt();
if(year<1900||year>11900){
System.out.println("输入错误");
}
System.out.println("输入月份");
month = input.nextInt();
System.out.println("输入天数");
day = input.nextInt();
//判断闰年
if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
yearDay = 366;
} else {
yearDay = 365;
}


for (k = 1900; k < year; k++) {
if (k % 400 == 0 || (k % 4 == 0 && k % 100 != 0)) {
wholeYearDay += 366;
} else {
wholeYearDay += 365;
}
}


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 (yearDay == 366) {
monthDay = 29;
break;


} else {
monthDay = 28;
break;
}


}
default:
System.out.println("月份输入错误");
System.exit(-1);
break;


}
if(day<=monthDay){
System.out.println("谢谢使用");
}else{
System.out.println("错误的天数");
System.exit(-1);
}



for (k = 1; k < month; k++) {


switch (k) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
wholeMonthDay += 31;
break;


case 4:
case 6:
case 9:
case 11:
wholeMonthDay += 30;
break;


case 2: {
if (yearDay == 366) {
wholeMonthDay += 29;


} else {
wholeMonthDay += 28;


}
break;
}
}


}

wholeDayDay = wholeYearDay + wholeMonthDay;

System.out.println("Mon    Tues    Wedn    Thur    Fri     Sat     Sun   ");
week = wholeDayDay % 7;

for (int j = 0; j < week; j++) {
System.out.print("\t");
}
for (int i = 1; i <= monthDay; i++) {
if ((i + week - 1) % 7 == 0) {
System.out.println();
}
if (i == day) {


System.out.print("¥"+i);
} else {
System.out.print(i + "\t");
}


}


}


}