定义:输入的年份(必须大于1990年)除以4等于0而且除以100不等于0,或者除以400等于0就是闰年,反之平年.
- 非整百年份除以4,整百年份除以400,如果不能被整除的是平年,可以被整除的就是闰年。
- 所谓闰年,就是指 2 月有 29 天的那一年。闰年同时满足以下条件:
1. 年份能被 4 整除。
2. 年份若是 100 的整数倍,须被 400 整除,否则是平年。
import java.util.Scanner;
public class RunPing {
/*
* 输入一个年份,判断是平年还是闰年
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入年份:");
int year = sc.nextInt();
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
System.out.println(year + "是闰年");
} else {
System.out.println(year + "是平年");
}
}
}
**输出结果:**
![在这里插入图片描述](https://img-blog.csdnimg.cn/20201229163800389.png)
![在这里插入图片描述](https://img-blog.csdnimg.cn/20201229163919731.png)