mercredi 23 mars 2016

invalid input while loop string

I'm writing a program for a museum ticket system where the user enters the exhibit they would like to visit however when there is invalid input the program skips the if statement and continues the rest of the program.

What im trying do here is make the program loop back to the start of the if statement when input data is invalid.

    while(invalidinput){
    if (userinput.equalsIgnoreCase("Roman")){
        System.out.println("There are " +availabletic.getAvailabletickets1()+ " tickets available for the "+exhibit.getExhibit1());
        System.out.println("");
    }
     else if (userinput.equalsIgnoreCase("Warhol Art")){
        System.out.println("There are " +availabletic.getAvailabletickets2()+ " tickets available for the "+exhibit.getExhibit2());
        System.out.println("");}

        else if (userinput.equalsIgnoreCase("World War 2")){
        System.out.println("There are " +availabletic.getAvailabletickets3()+ " tickets available for the "+exhibit.getExhibit3());
        System.out.println("");}


        else if (!userinput.equalsIgnoreCase("Roman")
            && !userinput.equalsIgnoreCase("Warhol Art") 
            && !userinput.equalsIgnoreCase("World War 2"))
invalidinput = false; // We signal that we may now leave the loop
        {
            System.out.println("Please enter a valid exhibition name");
        }               
  }

Aucun commentaire:

Enregistrer un commentaire