switch语句编写daysOfMonth方法(形参名称为month),返回每个月的天数)

img

public class Test {

public static void main(String[] args) {
    int month = 5;
    int day = daysOfMonth(month);
    System.out.println(month+"月是:"+day+"天");
}

public static int daysOfMonth(int month) {
    switch (month) {
    case 2:
        return 28;
        
    case 1:
    case 3:
    case 5:
    case 7:
    case 8:
    case 10:
    case 12:
        return 31;
    case 4:
    case 6:
    case 9:
    case 11:
        return 30;

    }
    return 0;
}

}

按照要求写12个case语句,主要是2月份要考虑闰年的情况,闰年返回29天。