mardi 4 mai 2021

why is the while loop executing twice?

Please help me understand why the while loop prompts the statement "enter am or pm" twice. I need it to ask only once and wait for an input. Only if the input in invalid, should the loop execute again.

String validAnswer = "False";

while (validAnswer.equals("False")){

  System.out.println("Enter am or pm: ");

  String time = input.nextLine();

  time = time.toLowerCase();

  if (time.equals("am") || time.equals("pm"))
  {
    
validAnswer="True";
  }

}

Aucun commentaire:

Enregistrer un commentaire