方法一、直接访问表
import java.util.Scanner;
import java.util.Calendar;
class DaysPerMonth {
public static void main(String[] args) {
System.out.println("输入年份:");
Scanner scan = new Scanner(System.in);
String input_year = scan.nextLine();
int year = Integer.parseInt(input_year);
System.out.println("输入月份:");
String input_month = scan.nextLine();
int month = Integer.parseInt(input_month);
int days = daysPerMonth(year, month);
System.out.println( year + " 年 " + month + " 月有 " + days + "天");
}
public static int daysPerMonth(int year, int month) {
boolean flag = LeapYearIndex(year);
System.out.println("是否是闰年?" + flag);
if(flag) {
int[] days = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
return days[month-1];
} else {
int[] days = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
return days[month-1];
}
}
public static boolean LeapYearIndex(int year) {
if ( ( year % 4 == 0 && year % 100 != 0 ) || year % 400 == 0 ) {
return true;
} else {
return false;
}
}
}