【问题标题】:Java - user input - loop on errorJava - 用户输入 - 错误循环
【发布时间】:2012-08-30 00:27:57
【问题描述】:

这是我的 Java 代码:

Scanner userInput = new Scanner(System.in);
while (true) { // forever loop
    try {
        System.out.print("Please type a value: "); // asks user for input
        double n = userInput.nextDouble(); // gets user input as a double
        break; // ends if no error
    }
    catch (Throwable t) { // on error
        System.out.println("NaN"); // not a number
    }
}

你可以从 cmets 看到这应该做什么。

但是当我输入不是数字的东西时,就会发生这种情况:

Please type a value: abc
NaN
Please type a value: NaN
Please type a value: NaN
Please type a value: NaN
Please type a value: NaN
Please type a value: NaN
Please type a value: NaN

以此类推,直到我强行停止它。

在 Python 中我会这样做:

while True:
    try:
        n = float(raw_input("Please type a value: "))
        break
    except Exception:
        print "NaN"

如何在 Java 中做到这一点? 我试过使用do while

【问题讨论】:

  • 不要抓Throwable,而是抓具体的InputMismatchException
  • 旁白:在 Python 中,您可能应该使用 ValueError 而不是 Exception

标签: java python loops while-loop nan


【解决方案1】:

如上所述, 你可以这样改变它:

    Scanner userInput = new Scanner(System.in);
    while (true) { // forever loop
        try {
            System.out.print("Please type a value: "); // asks user for input
            double n = userInput.nextDouble(); // gets user input as a double
            break; // ends if no error
        }
        catch (Throwable t) { // on error
            System.out.println("NaN"); // not a number
            userInput.nextLine();
        }
    }

【讨论】:

    【解决方案2】:

    catch 块中调用nextLine() 方法。

    将此扫描器前进到当前行并返回 被跳过。此方法返回当前行的其余部分, 不包括末尾的任何行分隔符。位置设置为 下一行的开头。

    由于此方法继续在输入中搜索 行分隔符,它可以缓冲所有搜索该行的输入 如果没有行分隔符则跳过。

     catch (InputMismatchException t) { // on error
        userInput.nextLine();
        System.out.println("NaN"); // not a number
      }
    

    【讨论】:

      【解决方案3】:

      试试这个,

      double n;
      boolean is_valid;
      
      do {
          try {
              System.out.print("Please type a value: ");
              Scanner kbd = new Scanner(System.in);
              n = kbd.nextDouble();
              is_valid = true;
          } catch (Exception e) {
              System.out.println("NaN"); 
              is_valid = false;
          }
      } 
      while (!is_valid);
      

      【讨论】:

        【解决方案4】:
        while (true) { // forever loop
            try {
                scanner userInput = new Scanner(System.in); 
                System.out.print("Please type a value: "); // asks user for input
                double n = userInput.nextDouble(); // gets user input as a double
                break; // ends if no error
            }
            catch (Throwable t) { // on error
                System.out.println("NaN"); // not a number
            }
        }
        

        你应该在 while 循环中使用扫描器类,如果给定的输入值错误,它只会询问下一个输入值。

        【讨论】:

          猜你喜欢
          • 2017-02-15
          • 2016-06-13
          • 1970-01-01
          • 1970-01-01
          • 2022-01-12
          • 1970-01-01
          • 2021-12-20
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多