Java黑皮书课后题第3章:**3.11(给出一个月的总天数)编写程序,提示用户输入月份和年份,然后显示这个月的天数。
import java.util.Scanner;
public class Q3_11 {
private static void DaysOfThisMonth(String month,int year) {
String Days31[] = {"January", "March", "May", "July", "August", "October", "December"};
for (int i = 0; i < 7; i++) {
if (month.equals(Days31[i])) {
System.out.print(month + " " + year + " has 31 days");
break;
}
else if (year % 4 == 0 & month.equals("February")) {
System.out.print(month + " " + year + " has 29 days");
break;
}
else {
System.out.print(month + " " + year + " has 30 days");
break;
}
}
}
public static void main(String[] args) {
System.out.print("Enter month and year: ");
Scanner input = new Scanner(System.in);
String month = input.next();
int year = input.nextInt();
DaysOfThisMonth(month,year);
}
}