0

I'm new on this java programming. And I need to make this program loop the Question "Do you want to Try Again?! [Yes/No]" and loop it back to "Enter any year:" question. How will I do that?

I hope you provided me with more elaborated answer or example to make this program work properly within today..

import java.util.Scanner;
public class my_LeapYear {

public static void main(String[] args) {
  Scanner s = new Scanner(System.in);
    System.out.print("Enter any year: ");
    int year = s.nextInt();
    boolean loop = true;
    while(loop == true)
{
    if (((year %4==0)&& (year %100 !=0))||(year %400==0))
    {
        System.out.println("Year "+year+" is a Leap Year");
        break;
    }
    else if ((year %100 == 0)&&(year %400 == 0))
{
} else {
        System.out.println("Year "+year+" is not a Leap Year");
        loop = false;
        break;
    }
Kishan Viramgama
  • 893
  • 1
  • 11
  • 23

1 Answers1

0

Try to use if statement that checks whether yes or no is entered , update the value of loop accordingly see below

   System.out.println("Do you want to Try Again")   
    String input = in.nextLine();
    if (input.equals("YES")){
        loop = true
    }
    else if (input.equals("NO")){
        loop=false
    }
The Scientific Method
  • 2,374
  • 2
  • 14
  • 25