A simple example of a leap year is judged by the Java program _java

Source: Internet
Author: User

We know that (1) if the whole hundred years, can be divisible by 400, is a leap year, (2) if not the whole hundred years, can be divisible by 4, is also a leap. Every 400 years, there are 97 leap year. In view of this, the procedure can be designed as follows:

The first step is to determine whether the year is divisible by 400 and, if possible, leap years. For example, 1600, 2000, and 2400 are leap years.

The second step is to determine whether the year is divisible by 100 and, if so, not leap years, on the basis of the first step not being established. For example, 1900, 2100, 2200 are not leap years.

The third step, on the basis of the second step, determines whether the year can be divisible by 4 and, if it is, leap years. For example, 1996, 2004, and 2008 are leap years.

The fourth step, on the basis of the third step, is not a leap year. For example, 1997, 2001, 2002 are not leap years.

Import java.util.scanner;//Insert Scanner public

class Runnian

{public

  static void Main (string[] args)//sting[] Args Don't forget to write it in.

  {

  Scanner s=new Scanner (system.in);//DECLARE scanner variable

  System.out.println ("Please enter Year");/system prompt to enter year

  the int nianfen=s.nextint ()///Gets the year value of the next line input

  if (nianfen%400==0) {System.out.println (nianfen+ years is a leap year);} To determine whether it is divisible by 400

  . else if (nianfen%100==0) {System.out.println (nianfen+ "Year is not leap years");} To determine whether or not divisible by 100

     else if (nianfen%4==0) {System.out.println (nianfen+ "Year is leap Years");} To determine whether or not divisible by 4

     else{system.out.println (nianfen+ "Year is not leap years");}}

After initial testing, this program can correctly determine whether it is a leap year. If there is any mistakes in this procedure, please treatise. We must have other ways to achieve this, welcome to reply to provide.

=======================

After learning about other people's video teaching, write a 2nd implementation method, you can use only one if-else statement. The code is as follows:

Import Java.util.Scanner;

public class Runnian

{public

  static void Main (string[] args)

  {

  Scanner s=new Scanner (system.in);

  System.out.println ("Please enter the Year");

  int Nianfen=s.nextint ();

  if (nianfen%4==0&&nianfen%100!=0| | nianfen%400==0) {System.out.println (nianfen+ "Years is a leap year");}

  The year can be divisible by 4 but cannot be divisible by 100, or the year can be divisible by 400

    else{system.out.println (nianfen+ "Year is not leap years");}}

The above is a small series for everyone to use the Java program to determine whether a leap year is a simple example of all the content, I hope that we support cloud Habitat Community ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.