这道题核心是如何判断是否为闰年?闰年判定有两种规则:
普通闰年:能被 4 整除但不能能被 100 整除
世纪闰年:能被 400 整除
这样这道题就能解决了
具体代码实现如下:
public class IsLeapYear {
public static void main(String[] args) {
int IsLeapYear = 0;
for (IsLeapYear = 1000; IsLeapYear <= 2000; IsLeapYear++) {
if (IsLeapYear % 4 == 0 && IsLeapYear % 100 != 0) {
//判定普通闰年
System.out.print(IsLeapYear + " ");
} else if (IsLeapYear % 400 == 0) {
//判定世纪闰年
System.out.print(IsLeapYear + " ");
}
}