【问题标题】:Requesting the same input if user enters an invalid option如果用户输入无效选项,则请求相同的输入
【发布时间】:2020-09-27 00:26:01
【问题描述】:
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
    }
  }
}

问题是我想这样做,所以如果他们输入任何其他字符,例如 Z 或整数,它会说“无效的选项,请再试一次” 循环回到同一个问题“你愿意重新开始?(y/n)"

【问题讨论】:

  • 根据您的代码,您无需为此做任何事情。它会在说“无效输入”后自动返回循环,因为您没有更新 i 的值,循环将继续。

标签: java repl.it


【解决方案1】:

在我说出答案之前有一件事:你不需要为 0 和 1 创建一个 int,有一个名为 boolean 的类型可以保持 false 或 true,它基本上类似于 0 和 1。

如果你想一次又一次地循环"Would you like to start again? (y/n) ",你可以这样做:

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
  }
}

如您所见,break 语句可以轻松跳出循环。

【讨论】:

    【解决方案2】:

    将所有 if/if else/else 语句放在 while (true) 中。如果输入有效,请在 if 语句中使用 break;。在这种情况下,您不需要使用else 语句。如果两个 if 语句都不为真,程序将继续请求输入。

    【讨论】:

      【解决方案3】:

      您可以添加另一个 while 语句。

        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
            }
          } 
        }
      }
      

      【讨论】:

        猜你喜欢
        • 2019-04-03
        • 2019-10-17
        • 1970-01-01
        • 2020-04-27
        • 2019-06-20
        • 2021-08-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多