闰年的判断规则如下:
(1)若某个年份能被4整除但不能被100整除,则是闰年。
(2)若某个年份能被400整除,则也是闰年。
package test;
import java.util.Scanner;
public class Test4 {
public static void main(String[] args) {
while (true) {
System.out.println("INPUT YEAR : ");
Scanner scanner = new Scanner(System.in);
int year = scanner.nextInt();
if (year < 0 || year > 3000) {
System.out.println("wrong year!!!");
} else {
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
System.out.println("閏年");
} else {
System.out.println("閏年ではない");
}
}
}
}
}
统计1900年后的闰年及总数
import java.util.Scanner;
public class Test4 {
public static void main(String[] args) {
try {
while (true) {
System.out.println("INPUT YEAR : ");
Scanner scanner = new Scanner(System.in);
int year = scanner.nextInt();
if (year < 0 || year > 3000) {
System.out.println("wrong year!!!");
} else {
int count = 0;
for (int i = 1900; i <= year; i++) {
if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) {
System.out.println(i+"年は閏年");
count++;
}
}
System.out.println("count is "+count);
}
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("INPUT CORRECT YEAR");
}
}
}