Advertisement
brilliant_moves

LeapYears.java

Sep 30th, 2015
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 1.07 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class LeapYears {
  4.  
  5.     /* Determine whether a particular year is a leap year or not and use this method
  6.      inside a new method to determine a subsequent leap year. */
  7.  
  8.     public static void main(String[] args) {
  9.  
  10.         Scanner scan = new Scanner(System.in);
  11.  
  12.         System.out.println("This program calculates leap years.");
  13.         System.out.print("Enter the year: ");
  14.         int year = scan.nextInt();
  15.  
  16.         if (year<1582) {
  17.             System.out.println("Error! Gregorian Calendar only started in 1582.");
  18.             return;
  19.         } // if
  20.  
  21.         if (isLeapYear(year)) {
  22.             System.out.println (year +" is a leap year.");
  23.         } else {
  24.             System.out.println (year +" is not a leap year.");
  25.         } // if
  26.  
  27.         year = findNextLeapYear(year);
  28.  
  29.         System.out.println ("The next leap year is " + year);
  30.  
  31.     } // main()
  32.  
  33.     public static int findNextLeapYear(int year) {
  34.         while (!isLeapYear(++year));
  35.         return year;
  36.     } // findNextLeapYear()
  37.  
  38.     public static boolean isLeapYear(int year) {
  39.          return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
  40.     } // isLeapYear()
  41.  
  42. } // class LeapYears
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement