
闰年的判断条件:
1:能被4整除,并且不能被100整除。
2:能被400整除。
System.out.println("请输入年份:");
String year;//接收输入的年份
Scanner scanner = new Scanner(System.in);
year = scanner.nextLine();
Pattern pattern = Pattern.compile("[0-9]{1,}");
Matcher matcher = pattern.matcher((CharSequence) year);
boolean result = matcher.matches();
if (result) {
int yearInt = Integer.parseInt(year);
if (yearInt % 4 == 0 && yearInt % 100 != 0 || yearInt % 400 == 0) {
System.out.println(year + "是闰年");
} else {
System.out.println(year + "不是闰年");
}
}else{
System.out.println("年份有误!");
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)