package com.liaojianya.chapter1; import java.util.Scanner; /** * This program demonstrates the way of judging leap year. * @author LIAO JIANYA * 2016年7月19日 */ public class LeapYear { public static void main(String[] args) { @SuppressWarnings("resource") Scanner scan = new Scanner(System.in); System.out.println("Please enter the year: "); int year = (int)scan.nextInt(); if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0) { System.out.println(year + " is leap year!"); } else { System.out.println(year + " is not leap year!"); } } }
运行结果1:
Please enter the year: 2004 2004 is leap year!
运行结果2:
Please enter the year: 2017 2017 is not leap year!
运行结果3:
Please enter the year: 4000 4000 is leap year!
分析:
判断闰年一般的规律为: 四年一闰,百年不闰,四百年再闰.
其简单计算方法:1.能被4整除而不能被100整除.(如2016年就是闰年,1800年不是.)
2.能被400整除.(如2000年和4000年都是闰年)