判断闰年

 
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年都是闰年)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值