当前位置: 首页 > 知识库问答 >
问题:

做而去无限与尝试捕捉

东门秦迟
2023-03-14

我在do while loop中尝试执行try-catch语句时遇到了一个问题,我要求用户首先输入字母,然后输入一个数字,如果他输入正确,程序结束;如果他输入字母而不是数字,程序应该说“发生错误,请输入数字”,并要求用户再次输入数字,但每次我输入字母而不是数字,程序就进入了一个无限循环,不允许我输入新的值。只是说“发生错误,您必须输入数字”“请输入数字”。

public class OmaBrisem {

    public static void main(String[] args) {
        Scanner tastatura = new Scanner(System.in);
        boolean b = true;
        int a = 0;
        String r = "";
        System.out.println("Please enter a letter");
        r = tastatura.next();
        do {
            try {
                System.out.println("Please enter numerical value");
                a = tastatura.nextInt();
                b = true;
            } catch (Exception e) {
                System.out.println("An error occured you must enter number");
                b = false;
            }
        } while (!b);

    }

}

共有1个答案

朱典
2023-03-14

你的问题来了。如果用户输入一个非数字,而您希望输入数字,则nextint()将引发异常,但不会从输入流中删除字母!

这意味着当您再次循环获取数字时,字母仍然存在,并且nextint()将再次引发异常。以此类推,无限(或至少直到宇宙热死,或机器最终崩溃,以先到者为准)。

解决这个问题的一种方法是在nextint()失败时实际读取/跳过下一个字符,以便将其从输入流中删除。基本上可以使用scanner.findinline(“.”)来完成,直到scanner.hasnextint()返回true。

import java.util.Scanner;
public class MyTestProg {
     public static void main(String [] args) {
         Scanner inputScanner = new Scanner(System.in);
         System.out.print("Enter letter, number: ");

         // Get character, handling newlines as needed

         String str = inputScanner.findInLine(".");
         while (str == null) {
             str = inputScanner.nextLine();
             str = inputScanner.findInLine(".");
         }

         // Skip characters (incl. newline) until int available.

         while (! inputScanner.hasNextInt()) {
             String junk = inputScanner.findInLine(".");
             if (junk == null) {
                 junk = inputScanner.nextLine();
             }
             System.out.println("Ignoring '" + junk + "'");
         }

         // Get integer and print both.

         int num = inputScanner.nextInt();
         System.out.println("Got '" + str + "' and " + num);
     }
}

下面的文字记录显示了它的作用:

Enter letter, number: Abcde42
Ignoring 'b'
Ignoring 'c'
Ignoring 'd'
Ignoring 'e'
Got 'A' and 42
 类似资料:
  • 我真的不知道这个问题。。。 如果数字不正确,块将捕获异常,当我输入-1或0时,它将捕获异常并要求我再次输入数字。。。但如果我键入类似asdasd的内容,它将运行一个无限循环。

  • 所以我正在构建一个程序,从用户输入中获取int。我有一个非常简单的try/catch块,如果用户没有输入int,应该重复这个块,直到他们输入int为止。以下是代码的相关部分: 如果我为第二个整数输入一个0,那么try/catch完全按照它应该做的做,并让我再次输入它。但是,如果我有一个InputMismatchException,比如为其中一个数字输入5.5,它只是在一个无限循环中显示我的错误消息

  • 我希望程序在捕获异常时重新执行 while 循环 - 异常正在接收文本输入。相反,它继续使用下面的代码进行一段时间循环,我希望它再次要求用户输入。 输出:

  • 我的项目由一个小图标组成,它在一个尺寸为25×20的网格上移动。我知道我可以用几个if/else块轻松地完成这项工作,但我想了解更多关于错误处理的知识。 我当时想的是使用try-catch,但它根本不会捕获数组索引越界异常或任何:它不会返回“error”或位置,因此它永远不会进入catch块。 我在想这样的伪代码: 实际代码:

  • 问题内容: 我正在尝试构建一个发出ajax请求的Google Chrome扩展程序。与GMail Checker扩展程序类似。问题是,当我使用jquery进行请求时,并输入了错误的用户名/密码时,它会静默失败,并忽略错误回调函数。 如果我将ajax调用从background.html脚本(在开发人员窗口中看不到请求)中移出到options.html脚本中,则会得到一个对话框来重新进行身份验证。如果

  • 问题内容: 因此,我正在构建一个从用户输入中获取整数的程序。我有一个看起来非常简单的try / catch块,如果用户不输入int,则应该重复该块直到他们这样做。这是代码的相关部分: 如果我为第二个整数输入0,那么try / catch会完全按照预期的方式运行,并让我再次放入。但是,如果我有一个InputMismatchException,例如为其中一个数字输入5.5,它只会在无限循环中显示我的错