jeudi 12 septembre 2019

Do loop runs twice before getting input, if statement doesn't evaluate condition [duplicate]

This question already has an answer here:

I am trying to run through the do loop once and get input then use the input for the if statement. The loop runs twice before getting any input and the if statement doesn't evaluate the condition, it runs every time regardless.

I have tried using != instead of .equalsIgnoreCase but neither works.

import java.util.Scanner;

 public class WDUnit4
 {
  public static void main(String args[])
  {
   Scanner stdIn = new Scanner(System.in);
   String dayOfFlight;
   String currentDay;
   int hourOfFlight;
   int currentHour;
   int timeLeft;  //time left before the flight

   System.out.print("What is the day of your flight (Monday-Sunday):");
   dayOfFlight = stdIn.nextLine();
   System.out.print("What hour does your flight leave (Military Time):");
   hourOfFlight = stdIn.nextInt();

   do 
   {
    System.out.print("What is the current day:");
    currentDay = stdIn.nextLine();
    if (!(currentDay.equalsIgnoreCase(dayOfFlight)))
    {
    System.out.print("Today isn\'t your flight, keep checking.");
    }
   } while (!(currentDay.equalsIgnoreCase(dayOfFlight)));




  }  //end main
 }  //end class WDUnit4

Aucun commentaire:

Enregistrer un commentaire