java输入一个月份求闰年
时间: 2024-01-23 22:09:07 浏览: 30
好的,我可以通过Java来实现输入一个月份求闰年的功能。代码如下:
```
import java.util.Scanner;
public class LeapYearDemo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个月份:");
int month = sc.nextInt();
System.out.println("是否闰年:" + isLeapYear(month));
sc.close();
}
public static boolean isLeapYear(int month) {
int year = 2021; // 假设当前年份为2021年
if (month < 1 || month > 12) {
System.out.println("请输入1~12之间的月份!");
return false;
}
if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
// 闰年的判断条件:能被400整除或者能被4整除但不能被100整除
if (month == 2) {
return true;
}
}
return false;
}
}
```
运行程序后,输入一个月份,程序会输出是否为闰年。其中,假设当前年份为2021年,如果要求输入的年份为其他年份,需要将上面的`year`变量修改为对应年份即可。