简体   繁体   中英

Requesting the same input if user enters an invalid option

public static void main(String[] args) {
  int i = 0;
  while (i==0) { //game restarts if i=0
    PlayGame(); // Resets back to the start after the game is finished.

    System.out.print("Would you like to start again? (y/n) ");
    String sAns = System.console().readLine();
    if (sAns.equalsIgnoreCase("y")) { //! means other
       System.out.println("Restart");
       i=0; // make it so they cant do another letter//
    }
    else if (sAns.equalsIgnoreCase("n")) {
       System.out.println("");
       i=1;  // breaks the loop
    }
    else {
       System.out.println("Invalid input.");
       // loop it back to the question
    }
  }
}

Well the problem is I'd like to make it so if they enter any other character such as Z or an Integer, it'll say "Invalid option please try again" Looping it back to the same question "Would you like to start again? (y/n)"

One thing before I say the answer: you don't need to create an int for 0 and 1, there is a type called boolean that can hold false or true which basically resembles 0 and 1.

If you want to loop "Would you like to start again? (y/n) " again and again, you can do this:

while(true) { // surround with while loop
System.out.print("Would you like to start again? (y/n) ");
  String sAns = System.console().readLine();
  if (sAns.equalsIgnoreCase("y")) {//! means other
    System.out.println("Restart");
    i=0;// make it so they cant do another letter//
    break;
  }
  else if (sAns.equalsIgnoreCase("n")) {
     System.out.println("");
    i=1;// breaks the loop
    break;
  }
  else {
    System.out.println("Invalid input.");
    //loop it back to the question
  }
}

As you can see, the break statement can easily break out of a loop.

Place all your if/if else/else statements inside while (true) . If the input is valid, use break; inside the if statement. In this case, you don't need to use an else statement. If neither of the if statements are true, the program will continue to ask for input.

You could add another while statement.

  public static void main(String[] args) {
  int i = 0;
  while (i==0) {//game restarts if i=0
    PlayGame();// Resets back to the start after the game is finished.

    boolean valid = false;
    while(!valid) {
      System.out.print("Would you like to start again? (y/n) ");
      String sAns = System.console().readLine();
      if (sAns.equalsIgnoreCase("y")) {//! means other
        System.out.println("Restart");
        i=0;// make it so they cant do another letter//
        valid = true;
      }
      else if (sAns.equalsIgnoreCase("n")) {
        System.out.println("");
        i=1;// breaks the loop
        valid = true;
      }
      else {
        System.out.println("Invalid input.");
        valid = true;
        //loop it back to the question
      }
    } 
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM