Java判断闰年
判断闰年条件有两个条件,分别为:
①非整百年数除以4,没有余数为闰年,有余数为平年(不是闰年);
②整百年数除以400,无余数为闰年有余平年。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个年份:");
//将年份放在自变量a中
int leapYear = sc.nextInt();
if (leapYear % 100 != 0) {
//有余数,非整百年
if (leapYear % 4 == 0) {
System.out.println("年份" + leapYear + "是闰年!");
} else {
System.out.println("年份" + leapYear + "不是闰年!");
}
} else {
//整百年
if (leapYear % 400 == 0) {
System.out.println("年份" + leapYear + "是闰年!");
} else {
System.out.println("年份" + leapYear + "不是闰年!");
}
}
}
}
结果如下图: