判断闰年

来源:互联网 发布:js 定时执行 编辑:程序博客网 时间:2024/05/22 16:53
 
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: 20042004 is leap year!

  运行结果2:

Please enter the year: 20172017 is not leap year!

  运行结果3:

Please enter the year: 40004000 is leap year!

  分析:

  判断闰年一般的规律为: 四年一闰,百年不闰,四百年再闰.

  其简单计算方法:1.能被4整除而不能被100整除.(如2016年就是闰年,1800年不是.)

          2.能被400整除.(如2000年和4000年都是闰年)

0 1
原创粉丝点击