hey everyone,
I'm trying to perform a program that will perform a leap year method and also have it to loop. here's what i have so far:
import java.util.Scanner;
public class LeapYear
{
//Create a Scanner object for keyboard input.
static Scanner keyboard = new Scanner(System.in);
//Set variables
public static void main(String[] args)
{
int year;
displayInstructions(); year = getYear();
displayResults(isLeap(year), year);
//Have the user to enter yes or no to get another year.
char Answer;
System.out.println();
System.out.print("Would you like to enter another year? y/n: ");
Answer = keyboard.next().charAt(0);
if (Answer =='n')
{
System.out.println("Thank you for entering a year!");
System.out.println();
}
else
year = getYear();
displayResults(isLeap(year), year);
}
//Display the instructions for the program.
public static void displayInstructions()
{
System.out.println("This program allows the user to enter a year then,");
System.out.println("the program determines whether or not the year entered");
System.out.println("is a leap year.");
System.out.println();
}
//Get the year from the user
public static int getYear()
{
int Year;
System.out.print("Please enter a year of your choice: ");
Year = keyboard.nextInt();
return Year;
}
//Determine if the year entered is leap year or not
public static boolean isLeap(int Year)
{
if (Year % 4 != 0)
{
return false;
}
else if (Year % 400 == 0)
{
return true;
}
if (Year % 100 == 0)
{
return false;
}
else
{
return true;
}
}
//Display the year as being either a leap year or not.
public static void displayResults(boolean LeapYear, int year)
{
if (isLeap(year))
{
System.out.println(" " + year + " is a leap year.");
}
else
{
System.out.println(" " + year + " is not a leap year.");
}
}
}
This code is to repeat until the user enter the letter n, which will terminate the program. I'm stuck at this point. Could anyone help.