甲骨文预科学习第三天

   20170222

    今日学习了如何调用函数来打印万年历
    public class MyCalendar {
public static void main(String[] args) {


int year =2017;

printYearCalendar(year);
}


// System.out.println(getMonthDays(year, month));
private static void printYearCalendar(int year) {
System.out.println(year + "年的日历");
for (int i = 1; i < 13; i++) {
int month = i;
printHeader(year, month);
printCalendarBody(year, month);
System.out.println();
}


}


// 抬头
static void printHeader(int year, int month) {


System.out.println(year + "年" + month + "月的日历");
System.out.println("日\t一\t二\t三\t四\t五\t六");
}


// 主体
static void printCalendarBody(int year, int month) {
int weekDays = getWeekDayofFirstDayofMonth(year, month);
for (int i = 0; i < weekDays; i++) {
System.out.print("\t");
}
int day = getMonthDays(year, month);
for (int i = 1; i < day + 1; i++) {
int currentDays = i;
System.out.print(currentDays + "\t");
if ((currentDays + weekDays) % 7 == 0) {
System.out.println();
}
             }
}

              private static int getWeekDayofFirstDayofMonth(int year, int month) {
int weekDays = 0;
int days = 0;
for (int i = 1900; i < year; i++) {
int currentYear = i;
if (isLeapYear(currentYear) == true) {
days = days + 366;
} else {
days = days + 365;
}
}
for (int i = 1; i < month; i++) {
int currentMonth = i;
days =days + getMonthDays(year, currentMonth);

}
weekDays = ((days % 7) + 1) % 7;
return weekDays;
}


// 某月的天数
private static int getMonthDays(int year, int month) {
int days;
switch (month) {
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 (isLeapYear(year) == true) {
days = 29;
} else {
days = 28;
}
break;
default:
days = 0;
break;
}
return days;
}


// 判断是否闰年
private static boolean isLeapYear(int year) {
boolean result;
if (year % 400 == 0 || year % 4 == 0 && year % 100 != 0) {
result = true;
} else {
result = false;
}
return result;
}
}

你可能感兴趣的:(甲骨文)