Sei sulla pagina 1di 3

//*******************************************************************

// Leapyear.java
//
// Calculates leap years using simple math and nested if-statements
//*******************************************************************

import java.util.Scanner; // imports the class Scanner from the Utilities Library

public class LeapYear // Defines the class as leapyear


{
//------------------------------------------------------// Determines if the date inputted is a leap year or not.
//------------------------------------------------------public static void main (String[] args) // Method header
{ // Open brace for method body
int inputyear; // Sets the variable year as an interger which is used to find if the
date inputted is a leap year or not.
Scanner scan = new Scanner (System.in); // Sets the program to allow the user to
input

System.out.println (" Enter the date of year that is after 1581 (0 to quit): ");
inputyear = scan.nextInt(); // Allows the user to input
while (inputyear != 0)
{
if (inputyear < 1582) // Sets so if the value zero is inputted, the system ends.
{ // The while loop that if the value zero is inputted the program doesnt work and
it results in an error
System.out.println (" Invalid (The Gregorian calendar was adopted on 1582):
"); // Prints out the statement in quotation
System.out.println();
System.out.println (" Enter the date of year that is after 1581 (0 to quit): ");

inputyear = scan.nextInt(); // Allows the user to input


}
else
{
if (inputyear % 4 != 0 && inputyear >= 1582)
{
System.out.println ("The year is not a leap year. ");
System.out.println ("Enter the date of year that is after 1581 (0 to quit): ");
inputyear = scan.nextInt();
}

if (inputyear % 4 == 0 && inputyear % 100 != 0)


{
System.out.println (" The year is a leap year. ");
System.out.println (" Enter the date of year that is after 1581 (0 to quit): ");
inputyear = scan.nextInt();
}

if (inputyear % 4 == 0 && inputyear % 100 == 0 && inputyear % 400 == 0)


{
System.out.println ("The year is a leap year. ");
System.out.println ("Enter the date of year that is after 1581 (0 to quit): ");
inputyear = scan.nextInt();
}

if (inputyear % 4 == 0 && inputyear % 100 == 0 && inputyear % 400 != 0)


{
System.out.println ("The year is not a leap year. ");
System.out.println ("Enter the date of year that is after 1581 (0 to quit): ");
inputyear = scan.nextInt();

}
}
}
System.out.println("Program has terminated.");
}
}

Potrebbero piacerti anche