【问题标题】:Catch Block Not Printing Statement of Invalid Input捕获块不打印无效输入的语句
【发布时间】:2016-09-27 21:51:26
【问题描述】:

我希望用户输入一个介于 0-9 之间的数字。如果他们输入了超出此范围的数字(负数或大于 9 的数字),则他们输入了无效输入。此外,我想使用 try catch 块来识别不正确的输入类型(字符串),并打印无效。

用户无需重新输入。一次就好。

到目前为止我所尝试的:

import java.util.InputMismatchException;
import java.util.Scanner;
import java.util.Random;

public class guessinggame
{
    public static void main (String[] args)
    {
        int randomNumber = new Random().nextInt(10);
        System.out.println("My number is " + randomNumber + ". ");

        System.out.println("I’m thinking of a number between 0 and 9.");
        System.out.println("What is your guess:");


        Scanner keyboard = new Scanner(System.in);
        int guess = keyboard.nextInt();

            try{
                input = keyboard.nextInt();

            }catch (InputMismatchException e){
                guess = keyboard.nextInt();
                System.out.println("Invalid.");
            }



           if (guess < randomNumber) {
                System.out.println("your guess was too low.");
           }else if (guess > randomNumber){
                System.out.println("your guess was too high.");
            }else if (guess == randomNumber){
                System.out.println("your guess was correct.");
            }else if (guess < 0){
                System.out.println("Invalid.");
            }else if (guess > 9){
                System.out.println("Invalid.");
          }
        }
    }

这只是我的 try catch 块:

            try{
                input = keyboard.nextInt();

            }catch (InputMismatchException e){
                guess = keyboard.nextInt();
                System.out.println("Invalid.");
            }

我遇到的问题是,在编译时,我输入一个字符串输入,例如“abc”,程序会创建这种格式的运行时错误:

Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:864)
    at java.util.Scanner.next(Scanner.java:1485)
    at java.util.Scanner.nextInt(Scanner.java:2117)
    at java.util.Scanner.nextInt(Scanner.java:2076)
    at guessinggame.main(guessinggame.java:17)

当所有 应该 在输入字符串后显示为: Invalid. 根据我的 catch 块中的声明 System.out.println("Invalid.");

【问题讨论】:

    标签: java validation types try-catch


    【解决方案1】:

    您的错误被抛出在这一行:int guess = keyboard.nextInt();,它没有被try/catch 语句包围。请注意,每次您拨打Scanner.nextInt() 时,您都要求换行。尝试这样做:

    int guess = -1; // This will be an invalid input
    try{
        guess = keyboard.nextInt();
    }catch (InputMismatchException e){
        // Because guess is already -1, "invalid" will be printed out by 
        // Below if statements
    }
    

    这样,如果用户输入了一个无效的猜测,你可以使用你已经创建的 if 语句来处理它。

    【讨论】:

    • 谢谢,您的解释很有见地,有助于我理解 try/catch 语句的主题。
    【解决方案2】:

    用户无需重新输入。一次就好了。

    您要求输入 三次

    int guess = keyboard.nextInt(); // here
    
    try{
        input = keyboard.nextInt(); // here
    
    }catch (InputMismatchException e){
        guess = keyboard.nextInt(); // and here
        System.out.println("Invalid.");
    }
    

    第一个是抛出异常。由于没有捕获到该异常,它正在终止应用程序。

    看来你只是想要这个:

    int guess;
    try {
        input = keyboard.nextInt();
    } catch (InputMismatchException e) {
        System.out.println("Invalid.");
        return;
    }
    

    代替catch 块末尾的return,您可以做其他事情,无论您想做什么都取决于您。但是,如果 guess 从未设置为有效的 int,则该方法中的其余逻辑将不适用,因此默认情况下我只是在此处退出该方法。

    但基本上,如果您希望用户输入一次,那么只要求输入一次。并且任何时候你想处理异常,将抛出异常的代码包装在try/catch 中来处理它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-18
      • 2014-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-23
      相关资源
      最近更新 更多