用java记事本完成以下要求,根据输入的年份和月份判断月份的天数。若月份不在有效范围之内,应提示:“月份输入不正确。”。月份不为2月,根据输入月份输出对应的月份天数。月份为2月,根据年份判断如为普通闰...
时间: 2024-05-15 11:13:55 浏览: 15
以下是Java代码实现:
```java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("请输入年份:");
int year = input.nextInt();
System.out.print("请输入月份:");
int month = input.nextInt();
if (month < 1 || month > 12) {
System.out.println("月份输入不正确。");
} else {
int days = 0;
if (month == 2) {
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
days = 29;
} else {
days = 28;
}
} else if (month == 4 || month == 6 || month == 9 || month == 11) {
days = 30;
} else {
days = 31;
}
System.out.println(month + "月的天数为:" + days);
}
}
}
```
运行结果示例:
```
请输入年份:2022
请输入月份:2
2月的天数为:28
```
```
请输入年份:2021
请输入月份:4
4月的天数为:30
```
```
请输入年份:2020
请输入月份:13
月份输入不正确。
```