Home >Java >javaTutorial >Example code sharing of how to determine whether a year is an ordinary year or a leap year in Java

Example code sharing of how to determine whether a year is an ordinary year or a leap year in Java

黄舟
黄舟Original
2017-06-04 09:25:155086browse

This article mainly introduces Java in detail to determine whether a given year is an ordinary year or a leap year. It has a certain reference value. Interested friends can refer to it

Requirements:

* Determine whether the year entered by the user is an ordinary year or a leap year

Implementation code:

import java.util.Scanner;

/**
 * 要求:
 *  判断用户输入的年份是平年还是闰年
 * @author Administration
 *
 */
public class Judge {
 public static void main(String[] args) {
  Scanner input = new Scanner(System.in);
  System.out.println("请输入一个年份:");
  long year = input.nextLong();
  //闰年需要满足的条件:能被4整除但不能被100整除,或者能被400整除,满足其中一个即可
  if((year%4==0 && year%100!=0) || year%400==0 ){
   System.out.println(year+"年是闰年!");
  }else{
   System.out.println(year+"年是平年!");
  }
 }
}

Running result:

Please enter a year:
1000
The year 1000 is an ordinary year!

Please enter a year:
2000
2000 is a leap year!

The above is the detailed content of Example code sharing of how to determine whether a year is an ordinary year or 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