Home  >  Article  >  Java  >  Determine whether it is a leap year in java

Determine whether it is a leap year in java

尚
Original
2019-11-21 16:30:366670browse

Determine whether it is a leap year in java

The judgment rules for leap years are as follows:

(1) If a certain year is divisible by 4 but not divisible by 100, it is a leap year.

(2) If a certain year is divisible by 400, it is also a leap year.

package com.learn01;
 
import java.util.Scanner;
/**
 * 判断输入年份是否是闰年 满足条件
 * 1、能被4整除,不能被100整除
 * 2、能被400整除
 * @author Administrator
 *
 */
public class Test04 {
	public static void main(String args[]) {
		Scanner scan=new Scanner(System.in);
		System.out.println("请输入年份");
		int year=scan.nextInt();
		if(year%4==0 && year%100!=0) {
			System.out.println(year+"是闰年");
		}else if(year%400==0) {
			System.out.println(year+"是闰年");
		}else {
			System.out.println(year+"不是闰年");
			
		}
		scan.close();
	}
}

The above code uses the year%4==0 && year 0!=0 and year@0==0 statements to determine whether it is a leap year.

For more java knowledge, please pay attention to java basic tutorial.

The above is the detailed content of Determine whether it is a leap year in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn