Skip to content

Instantly share code, notes, and snippets.

@imryan
Last active December 26, 2015 21:49
Show Gist options
  • Save imryan/7218149 to your computer and use it in GitHub Desktop.
Save imryan/7218149 to your computer and use it in GitHub Desktop.
Determines leap years.
import java.util.Scanner;
public class Leap {
public static void main(String[] args) {
// Call menu method
menu();
}
public static void menu()
{
// Declare variables
Scanner sc = new Scanner(System.in);
int year = 0;
// Output instructions
System.out.println("[1] Enter year\n[2] Quit");
// Read in value for year
System.out.println("\nEnter year: ");
year = sc.nextInt();
if (year == 2)
{
System.exit(1);
}
else if (year < 1582)
{
// Output error with inputted year
System.out.println("Error with year.\n");
menu();
} else {
// Output results of isLeapYear() method
System.out.println(year + isLeapYear(year) + "\n");
menu();
}
menu();
}
public static String isLeapYear(int year)
{
// Check if year divides into 4 evenly
if (year % 4 == 0)
{
return " is a leap year.";
}
// Check if year divides into 100 evenly
else if (year % 100 == 0)
{
return " is not a leap year.";
}
return " is not a leap year.";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment