请填写程序(Exam1.java)。该程序要求在Exam1.java中实现方法getDays来返 回某牛月的具体天数,并对2024年2月进行测试。
时间: 2024-02-23 20:01:40 浏览: 29
下面是Exam1.java的代码:
```java
public class Exam1 {
public static void main(String[] args) {
int year = 2024;
int month = 2;
int days = getDays(year, month);
System.out.println(year + "年" + month + "月有" + days + "天");
}
public static int getDays(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 (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
days = 29;
} else {
days = 28;
}
break;
default:
days = 0;
break;
}
return days;
}
}
```
该程序中的getDays方法接受年份和月份作为参数,并返回该月的天数。在主方法中,我们将2024年2月的年份和月份传递给getDays方法,并将返回的天数打印到控制台上。在本例中,由于2024年是闰年,2月份有29天,因此输出为“2024年2月有29天”。