【问题标题】:Why is my While Loop wont loop my Catch, like my if else statement?为什么我的 While 循环不会像我的 if else 语句那样循环我的 Cat?
【发布时间】:2021-11-08 04:47:40
【问题描述】:

我正在尝试制作一个程序,用户将在其中输入随机整数/数字。但主要问题是,如果用户不小心输入了一个字符串,我希望他们得到通知,并且我希望他们通过再次询问来再次要求输入。但在我的代码中,我无法循环捕获。

public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);

    int answer = 28;
    int attempts = 0;
    
    System.out.println("Guess a Number 1 - 50:");
    
    while(true) {
      try {
        int input = scan.nextInt();
        scan.nextLine();
        if(input > answer) {
          System.out.println("Too Big");
          System.out.println("Guess a Number 1 - 50:");
        } else if (input < answer) {
          System.out.println("Too Small");
          System.out.println("Guess a Number 1 - 50:");
        } else if (input == answer ) {
          System.out.println("Congrats!");
        } 
      } catch (InputMismatchException e) {
        System.out.println("Numbers only");
        System.out.println("Guess a Number 1 - 50:");
      } 
    }
}

【问题讨论】:

标签: java


【解决方案1】:

从 try 块中删除 scan.nextLine();,然后在 catch 块中放置 scan.skip(".*");。这将跳过无效输入(.* 基本上匹配任何字符)。此外,您可以用简单的else 替换最后一个else if,因为这是唯一的可能性。话虽如此,请尝试以下操作:

public static void main(String[] args) throws IOException {
    Scanner scan = new Scanner(System.in);

    int answer = 28;
    int attempts = 0;

    System.out.println("Guess a Number 1 - 50:");

    while(true) {
        try {
            int input = scan.nextInt();
            if(input > answer) {
                System.out.println("Too Big");
                System.out.println("Guess a Number 1 - 50:");
            } else if (input < answer) {
                System.out.println("Too Small");
                System.out.println("Guess a Number 1 - 50:");
            } else {
                System.out.println("Congrats!");
            }
        } catch (InputMismatchException e) {
            System.out.println("Numbers only");
            System.out.println("Guess a Number 1 - 50:");
            scan.skip(".*");
        }
    }
}

【讨论】:

  • @PabloJob,这有帮助吗?如果有不清楚的地方请告诉我。
【解决方案2】:

你问一个原因

此代码在遇到无效输入时会无限循环,因为nextInt() 不接受无效令牌,因此无效令牌会留在扫描仪中,因为它不会因异常而被消耗,不断导致抛出异常下次您尝试读取 int 时,因为由于异常而永远无法到达 nextLine

这可以通过将nextLine() 放在 catch 块中来解决任何导致引发异常的输入,清除输入流并允许用户再次输入。

    public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);

    int answer = 28;
    int attempts = 0;
    
    System.out.println("Guess a Number 1 - 50:");
    
    while(true) {
      try {          
        int input = scan.nextInt();
        scan.nextLine();  // never reached when exception is thrown
        if(input > answer) {
          System.out.println("Too Big");
          System.out.println("Guess a Number 1 - 50:");
        } else if (input < answer) {
          System.out.println("Too Small");
          System.out.println("Guess a Number 1 - 50:");
        } else if (input == answer ) {
          System.out.println("Congrats!");
        } 
      } catch (InputMismatchException e) {
        scan.nextLine(); // This skips the invalid token 
        System.out.println("Numbers only");
        System.out.println("GuessNumber 1 - 50:");
      } 
    }
}

【讨论】:

    【解决方案3】:

    你应该试试这个代码,它会完美地满足你的要求......

    
         public static void main(String[] args) {
            int answer = 28;
            System.out.println("Guess a Number 1 - 50:");
            boolean condition = true;
            while (condition) {
                try {
                        Scanner scan = new Scanner(System.in);
                        int input = scan.nextInt();
                        scan.nextLine();
                        if (input > answer) {
                            System.out.println("Too Big");
                            System.out.println("Guess a Number 1 - 50:");
                        } else if (input < answer) {
                            System.out.println("Too Small");
                            System.out.println("Guess a Number 1 - 50:");
                        } else {
                            System.out.println("Congrats!");
                            condition=false;
                        }
                } catch (Exception e) {
                        System.out.println("Numbers only");
                        System.out.println("Guess a Number 1 - 50:");
                }
            }
        }
    
    

    【讨论】:

    • 如果您提供一些解释,这个答案可能会得到改进 - 您修复了什么,为什么您认为这会解决原始问题?另外,我不确定你是否测试过。
    • 抱歉没有完全测试,我已经更改了部分代码,现在它可以按要求完美运行,请通过它,谢谢您的反馈。
    • 你还没有提供任何解释。另外,当用户输入不是数字的内容时,您真的确定它正在工作吗?
    猜你喜欢
    • 2012-11-07
    • 2015-11-08
    • 2017-04-01
    • 1970-01-01
    • 2021-11-24
    • 2019-04-24
    • 1970-01-01
    • 2021-12-02
    • 2012-06-18
    相关资源
    最近更新 更多