抛出异常Java

时间:2020-05-14 15:01:55

标签: java try-catch

我正在练习使用try-catch块来验证用户输入。如此努力地做着,但是不知道为什么它不起作用。我尝试了两种方式。它总是弹出InputMismatchException并结束程序。

第一个,我跟随此视频:https://www.youtube.com/watch?v=PWez5mVXACc&t=356s

public int moveStone(int initialStone, int upperBound, int stoneBalance) throws Exception {
    int takeStone = 0;
    boolean isNumber = false;

    do {
        if (in.hasNextInt()){
            takeStone = in.nextInt();
            isNumber =true;
        } 
        if (!in.hasNextInt()) {
            if (stoneBalance >= upperBound) {
                System.out.println("Invalid move. You must remove between 1 and " + upperBound + " stones.\n");
                isNumber = false;
                takeStone = in.nextInt();
            }

            if (stoneBalance < upperBound) {
                System.out.println("Invalid move. You must remove between 1 and " + stoneBalance + " stones.\n");
                isNumber = false;
                takeStone = in.nextInt();
            }
        }
    } while (!(isNumber));
    return takeStone;
}   

这,接着是其他教程:

public int moveStone(int initialStone, int upperBound, int stoneBalance) throws Exception {
    int takeStone = 0;
    try {
        if (in.hasNextLine()) {
            throw new Exception();
        } else {
            takeStone = in.nextInt();
            return takeStone;
        }

    } catch (Exception e) {
        System.out.println("Invalid move");
        if (stoneBalance >= upperBound) {
            System.out.println("You must remove between 1 and " + upperBound + " stones.\n");
            takeStone = in.nextInt();
            return takeStone;
        }

        if (stoneBalance < upperBound) {
            System.out.println("You must remove between 1 and " + stoneBalance + " stones.\n");
            takeStone = in.nextInt();
            return takeStone;
        }
    } 
    return -1;
}

2 个答案:

答案 0 :(得分:1)

您要掷serverless.yml

aws-apigw-blahblah.com/dev/stylesheets/custom.css

仅捕获 events: - http: path: / method: GET cors: true - http: path: stylesheets method: GET cors: true 。在Exception

中使用if (in.hasNextLine()) { throw new Exception(); }
InputMismatchException

答案 1 :(得分:0)

问题出在哪里:用户输入仍然保留在扫描仪缓冲区中。

  1. 当它通过无效输入时,将转到catch块。此操作正确。
  2. 但是,当程序必须提示用户提供其他输入时,无效的输入仍在扫描器中,它直接进入takeStone = in.nextInt()。该程序直接由于InputMismatchException而跳出。

解决方案是在提示用户输入之前,让另一个扫描仪获取令牌。

public int moveStone(int initialStone, int upperBound, int stoneBalance) throws Exception {
    int takeStone = 0;
    try {
        if (!in.hasNextInt()) {
            throw new Exception();
        } else {
            takeStone = in.nextInt();
            return takeStone;
        }

    } catch (Exception e) {
        if (stoneBalance >= upperBound) {
            System.out.println("Invalid move. You must remove between 1 and " + upperBound + " stones.\n");
            in.next(); // input still in the Scanner buffer, use another scanner to take this input
            takeStone = in.nextInt();
            return takeStone;
        }

        if (stoneBalance < upperBound) {
            System.out.println("Invalid move. You must remove between 1 and " + stoneBalance + " stones.\n");
            in.next();
            takeStone = in.nextInt();
            return takeStone;
        }
    } 
    return -1;
}
相关问题