I already know how to check for a leap year, like this:
import java.util.*;
public class LeapYear {
public static void main(String[] args) {
int year;
{
Scanner scan = new Scanner(System.in);
System.out.println("Enter year: ");
year = scan.nextInt();
if ((year % 4 == 0) && year % 100 != 0) {
System.out.println(year + " is a leap year.");
} else if ((year % 4 == 0) && (year % 100 == 0)
&& (year % 400 == 0)) {
System.out.println(year + " is a leap year.");
} else {
System.out.println(year + " is not a leap year.");
}
}
}
}
But now I want to repeat this code. I've seen repeating code snippets before, and I can use just about any successfully, but this one is giving me trouble.
import java.util.Scanner;
public class LeapUpgrade
{
public static void main(String[] args)
{
String another = "y";
int year;
Scanner scan = new Scanner(System.in);
while (another.equalsIgnoreCase("y"))
{
System.out.println("Enter year: ");
year = scan.nextInt();
if ((year % 4 == 0) && year % 100 != 0)
{
System.out.println(year + " is a leap year.");
System.out.print("test another (y/n)? ");
another = scan.nextLine();
}
else if ((year % 4 == 0) && (year % 100 == 0)
&& (year % 400 == 0))
{
System.out.println(year + " is a leap year.");
System.out.print("test another (y/n)? ");
another = scan.nextLine();
}
else
{
System.out.println(year + " is not a leap year.");
System.out.print("test another (y/n)? ");
another = scan.nextLine();
}
}
}
}
What am I doing wrong here? Thanks in advance for your help and don't judge.