NewCodertheshooter
NewCodertheshooter

Reputation: 45

Find a leap year using a while or do-while loop

I have an assignment to ask people to input a code and find out if it is a leap year. Whatever the outcome is it asks the person if they would like to try a new input. If the person answers yes it asks you to enter a new year. If the person says no it signs the code off by printing a goodbye.

The only requirement is using a while or do-while loop. I am stuck.

import java.util.Scanner;

public class homework7 {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter a year: ");
        int year = scan.nextInt();
        //while (yes==1) {
        do {
             /**System.out.print("Enter a year: "); //prompting the user to enter information
             int year = scan.nextInt();*/


             if (year % 4==0) {
                 System.out.println(year + " is a leap year"); 

                 System.out.println("Would you like to check another year? Enter 1 for yes, 0 for no");
                 int yes = scan.nextInt();
                 
                

             }
             else {
                 System.out.println(year + " is not a leap year");
             }
        }
        
        while(year % 4==0); 

    }

}

PS. Most importantly I'm stuck on figuring how to my the loop run again because I'm able to find out whether it is a leap year or not but when asked to continue I'm not sure how to make the input run again and ask the users for a new year!

Upvotes: 1

Views: 2227

Answers (2)

Tahir Hussain Mir
Tahir Hussain Mir

Reputation: 2626

Try this. Your Logic is wrong. Try to compare and understand.
If your while loop depends on user input (1 or 0), then why are you checking for year % 4 == 0?
Some tips: First: visualize your code and draw a flowchart. Don't just read the problem and write the code. Think, visualize and then write

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    int yes=1;
    
    while(yes == 1){
        System.out.println("Enter a year: ");
        int year = scan.nextInt();
        if (year % 4==0) {

             if( year % 100 == 0){

               // year is divisible by 400, hence the year is a leap year
               if ( year % 400 == 0)
                   System.out.println(year + " is a leap year"); 
               else
                   System.out.println(year + " is not a leap year"); 
             }
             else{
                   System.out.println(year + " is a leap year");   
             }
        else {
             System.out.println(year + " is not a leap year");
        }

         System.out.println("Would you like to check another year? Enter 1 for yes, 0 for no");
         yes = scan.nextInt(); //IF USER ENTERS ANY OTHER NUMBER OTHER THAN 0 & 1, IT WILL STILL EXIT. Leaving upto you
    } 

}

Upvotes: 1

Mustafa Poya
Mustafa Poya

Reputation: 3027

a leap year need this condition: year % 4 == 0 && (year % 100 == 0 && year % 400 == 0).

Scanner scan = new Scanner(System.in);
int year = -1;

while (year != 0) {
    System.out.println("Enter a year or 0 to end: ");
    year = scan.nextInt();

    if (year != 0) {
        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");
        }
    }
}

Upvotes: 1

Related Questions