小知识,大挑战!本文正在参与「程序员必备小知识」创作活动
闰年的判断规则如下:
(1) 若某个年份能被 4 整除但不能被 100 整除,则是闰年。
(2) 若某个年份能被 400 整除,则也是闰年。
package csdncom.tt;
import java.util.Scanner;
/**
* Created by Administrator on 2021/10/30.
*/
public class LeapYear {
public static boolean isLeapYear(int year) {
return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);
}
/**
* @param args
*/
public static void main(String[] args) {
System.out.print("请输入要查询的年份[整数]:");
Scanner input = new Scanner(System.in);
int year;
//获得键盘输入
if (input.hasNextInt()) {
year = input.nextInt();
} else {
System.out.println("输入数据格式不正确!");
System.exit(0);
}
if (isLeapYear(2000)) {
System.out.println("是闰年");
} else {
System.out.println("不是闰年");
}
}
}