java函数输入月份查天数
时间: 2024-06-15 09:04:57 浏览: 71
在Java中,可以通过编写一个函数来输入月份并返回对应的天数。以下是一个示例代码:
```java
public class MonthDays {
public static int getDaysInMonth(int month) {
int days = 0;
switch (month) {
case 1: // January
case 3: // March
case 5: // May
case 7: // July
case 8: // August
case 10: // October
case 12: // December
days = 31;
break;
case 4: // April
case 6: // June
case 9: // September
case 11: // November
days = 30;
break;
case 2: // February
days = 28;
break;
default:
System.out.println("Invalid month!");
}
return days;
}
public static void main(String[] args) {
int month = 2; // 输入月份,这里以2为例
int days = getDaysInMonth(month);
System.out.println("The number of days in month " + month + " is " + days);
}
}
```
上述代码中,`getDaysInMonth`函数接受一个整数参数`month`,根据输入的月份返回对应的天数。在`main`函数中,我们可以调用`getDaysInMonth`函数并打印结果。
阅读全文