-1

How would you get it to ask you to input Y or N? I have been working on this for a few days now. I am new to Java coding and I am taking AP Computer Science Principles. Thanks for the Help and if you could leave any tips about how I could improve that would be very helpful and appreciated.

import java.util.Scanner;
public class Project_3_3 {

public static void main(String[] args) 
{
    int year;
    boolean leap = false, num1;
    String answer = "y";
    Scanner Scan = new Scanner(System.in);

    while (answer.equals("y"))
    {

        System.out.println("Enter in a year greater or equal to 1582 ");
        year = Scan.nextInt();

        if (year>1582) {
            if ((year % 400==0 && year %100 !=0) || (year % 4 ==0 && year % 
           `enter code here`400==0)
                || year %4 ==0 && year % 100 !=0)
                System.out.println(year + " is a leap year");
            else 
                System.out.println(year + " is not a leap year");

            System.out.println();
            System.out.println("Test another year Y/N");
            answer = Scan.nextLine();
        }
    }
}
Komal12
  • 3,119
  • 4
  • 13
  • 25

1 Answers1

0

Just add an extra statement Scan.nextLine(); right before the statement answer = Scan.nextLine();. The reason why your program is exiting without taking user's input on Y/N is that the new line character entered during year = Scan.nextInt(); is consumed by answer = Scan.nextLine();.

VHS
  • 8,698
  • 3
  • 14
  • 38