Java抛出了一个我认为不应该出现的异常

Java抛出了一个我认为不应该出现的异常,java,exception,crash,try-catch,Java,Exception,Crash,Try Catch,我想做的是: 它要求输入int,但当有人输入chars时,它应该再次询问,而不是崩溃。第二个sticks=startInput.nextInt()不在try catch块内,因此如果放置更多字符,它将再次失败。由于您不是自己处理异常,因此异常将冒泡并最终导致应用程序崩溃 编辑:根据您的评论,视情况而定。假设您希望在用户提供0作为您问题的答案时关闭应用程序,您可以这样做: System.out.println("\nHow many sticks do you want?"); while

我想做的是:
它要求输入int,但当有人输入chars时,它应该再次询问,而不是崩溃。

第二个
sticks=startInput.nextInt()
不在
try catch
块内,因此如果放置更多字符,它将再次失败。由于您不是自己处理异常,因此异常将冒泡并最终导致应用程序崩溃

编辑:根据您的评论,视情况而定。假设您希望在用户提供
0
作为您问题的答案时关闭应用程序,您可以这样做:

System.out.println("\nHow many sticks do you want?");
    while(sticks==0){
        try{
            sticks=startInput.nextInt();
        }catch(InputMismatchException e){
            sticks=0;
            System.out.println("Please enter a valid number.");
        }
        sticks=startInput.nextInt();
    }
如果希望应用程序从代码中停止(这似乎不太可能),请执行以下操作:

System.out.println("\nHow many sticks do you want?");
    while(sticks >= 0){
        try{
            sticks=startInput.nextInt();
        }catch(InputMismatchException e){
            sticks=0;     //Any value which is not 0 will not break your loop. This will be re-populated when the user will supply the number again.
            System.out.println("Please enter a valid number.");
        }
    }
这是有效的:

System.out.println("\nHow many sticks do you want?");
    while(sticks==0){
        try{
            sticks=startInput.nextInt();
        }catch(InputMismatchException e){
            sticks=0;     //The 0 will break your while loop, exiting your application gracefully.
            System.out.println("Please enter a valid number.");
        }
    }

似乎您缺少在此处放置循环,请检查以下代码:

private static int sticks;
    private static Scanner startInput;

    public static void main(String[] args) {
        sticks = 0;
        startInput = new Scanner(System.in);        
        while (sticks >= 0) {            
            try {
                System.out.println("How many sticks do you want?");
                sticks = Integer.parseInt(startInput.nextLine());
            } catch (NumberFormatException e) {
                sticks = 0;     //Any value which is not 0 will not break your loop. This will be re-populated when the user will supply the number again.
                System.out.println("Please enter a valid number.");
            }
        }
    }

好的,我该怎么修复它?移除第二个line@TheEisbaer:我已经更新了我的答案。如果您有进一步的疑问,请告诉我。@JamesHutchinson:只需删除第二个
sticks=startInput.nextInt()
将使应用程序优雅地退出,我不认为OP在追求这一点。我想他/她会一直询问用户,直到提供令人满意的输入。对不起,我认为这说明我自己有点不好。我试着这样做:程序要求用户键入一个数字,用户没有键入一个数字,因为他打错了,然后程序应该再次询问。在您的第一个解决方案中,我的程序仍然会退出,或者我必须在退出后进行一些操作吗?如果我没有键入一个数字,那么它会开始发送垃圾邮件“请输入一个有效的数字”。但它会问用户一次,然后用户应该再次键入一个数字。假设您希望用户在终止之前获得n(比如3)次尝试次数,您可以按如下方式执行此操作:int attemptNum=0;while(attemptNum)
while(true){
    System.out.println("\nHow many sticks do you want?");
        while(sticks==0){
            try{
                sticks=startInput.nextInt();
            }catch(InputMismatchException e){
                sticks=0;
                System.out.println("Please enter a valid number.");
            }
        }
    }