所谓闰年,就是指 2 月有 29 天的那一年。闰年同时满足以下条件:
年份能被 4 整除。年份若是 100 的整数倍,须被 400 整除,否则是平年。
例如,1900 年能被 4 整除,但是因为其是 100 的整数倍,却不能被 400 整除,所以是平年;而 2000 年就是闰年;1904 年和 2004 年、2008 年等直接能被 4 整除且不能被 100 整除,都是闰年;2014 是平年。
package Test;
import java.util.Scanner;
public class Test4 {
// 在 main() 方法中编写 Java 代码,获取用户输入的年份和月份,其实现代码如下:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入年份(注: 必须大于 1990 年):");
int year = sc.nextInt();
System.out.println("请输入月份:");
int month = sc.nextInt();
test(year, month);
}
/**
* 判断闰年平年并输出某月的天数
*
* @param year
* @param month
* @return
*/
public static Integer test(Integer year, Integer month) {
boolean isRen;
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
System.out.println(year + "闰年");
isRen = true;
} else {
System.out.println(year + "平年");
isRen = false;
}
int day = 0;
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
day = 31;
break;
case 4:
case 6:
case 9:
case 11:
day = 30;
break;
default:
if (isRen) {
day = 29;
} else {
day = 28;
}
break;
}
System.out.println(year + "年" + month + "月共有" + day + "天");
return day;
}
}
输出结果:
请输入年份(注: 必须大于 1990 年):
2021
请输入月份:
2
2021平年
2021年2月共有28天
|