0

I'm having trouble with the sentinel-controlled loop structure of the lab I am in. I am supposed to make a program that can determine whether a year is a leap-year or a regular year. So far my professor said I have the boolean logic and the input ok, but I'm clueless how to set a sentinel controlled while loop that keeps on asking for an input until the user provides the program with the value that triggers the EOF command and terminates the program.

import java.util.Scanner;

public class LeapYear {
    public static void main(String[] args) {  
        Scanner S = new Scanner(System.in);
        System.out.println("Enter a year (0 to end)?");
        int year = S.nextInt();
        if (year > 1752) {
            if (((year%4)== 0) && ((year%100)==0) && ((year%400)==0)) {
                System.out.print("Leap Year");
            } else if (((year%4)== 0) && ((year%100)==0) && ((year%400)!=0)) {
                System.out.println("Regular Year");
            } else if ((year%4)== 0) {
                System.out.print("Leap Year");
            }
            else 
                System.out.print("Regular Year");
        }
        else 
            System.out.print ("Please enter a year after 1752");
    }
}
Lahiru
  • 1,428
  • 13
  • 17
Joel Mesa
  • 1
  • 1

1 Answers1

0

You have multiple solution, in your case the most suitable could be a do..while statement:

do
{
  // your code
}
while (condition); // condition to keep looping: input year not zero
Jack
  • 131,802
  • 30
  • 241
  • 343
  • Just to add to this answer: you will need to set a variable `true` or `false` depending on whether you had a valid input or not, and test for that variable in the `condition`. The `// your code` would be most of the stuff you have in your `main` program right now. – Floris Sep 19 '13 at 02:21