【问题标题】:While - try - catch in JavaJava中的while-try-catch
【发布时间】:2015-02-21 16:04:25
【问题描述】:

我需要一个 java 程序来询问 0 到 2 之间的数字。如果用户写 0,程序结束。如果用户写 1,它执行一个函数。如果用户写 2,它会执行另一个函数。我还想用一条消息处理错误“java.lang.NumberFormatException”,在这种情况下,再次询问用户一个数字,直到他写一个介于 0 和 2 之间的数字

我用

public static void main(String[] args) throws IOException {
    int number = 0;
    boolean numberCorrect = false;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));



        while (numberCorrect == false){
            System.out.println("Choose a number between 0 and 2");
            String option = br.readLine();
            number = Integer.parseInt(option);

            try {
                switch(option) {
                case "0":
                    System.out.println("Program ends");
                    numberCorrect = true;
                    break;
                case "1":
                    System.out.println("You choose "+option);
                    functionA();
                    numberCorrect = true;
                    break;
                case "2":
                    System.out.println("You choose  "+option);
                    functionB();
                    numberCorrect = true;
                    break;
                default:
                    System.out.println("Incorrect option");
                    System.out.println("Try with a correct number");
                    numberCorrect = false;
                }   
            }catch(NumberFormatException z) {
                System.out.println("Try with a correct number");
                numberCorrect = false;
            }
        }
    }

但是对于这段代码,catch(NumberFormatException z) 不起作用,并且程序不再要求输入数字。

【问题讨论】:

  • 答案已经给出。我认为您可以使用 Scanner 而不是 BufferedReader。

标签: java while-loop try-catch numberformatexception


【解决方案1】:

你永远不会在这里真正捕捉到NumberFormatException。您的代码基本上可以:

while (...) {
    // this can throw NumberFormatException
    Integer.parseInt(...)

    try {
        // the code in here cannot
    } catch (NumberFormatException e) {
        // therefore this is never reached
    }
}

你想在这里做的是:

while (!numberCorrect) {
    line = br.readLine();
    try {
        number = Integer.parseInt(line);
    } catch (NumberFormatException ignored) {
        continue;
    }

    // etc
}

【讨论】:

    【解决方案2】:

    您可以像这样在 parseInt 周围放置 try/catch:

    while (numberCorrect == false){
       System.out.println("Choose a number between 0 and 2");
       String option = br.readLine();
    
       try {
            number = Integer.parseInt(option);
        }catch(NumberFormatException z) {
           System.out.println("Try with a correct number");
           numberCorrect = false;
           option = "-1";
        }
    
        switch(option) {
            case "0":
            System.out.println("Program ends");
            numberCorrect = true;
            break;
    ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-09
      • 2011-10-31
      • 1970-01-01
      • 1970-01-01
      • 2015-04-24
      相关资源
      最近更新 更多