首页 > 解决方案 > 在 If 语句中使用临时值继续尝试 Catch

问题描述

我需要输入到数组中的值是 1-100 而不是任何字母。我试图设置一个临时值,该值将捕获之前不是 1-100 但似乎无法使其工作的数字。如果输入字母,我会遇到程序将关闭的问题。任何帮助,将不胜感激。

public class FocusGroup {
    static Scanner scan = new Scanner(System.in);

    public static void main(String args[]) {

        double tempValue = scan.nextDouble();

        if (tempValue > 100 || tempValue < 0) {
            scan.nextDouble();
            System.out.println("Invalid");

        } else {

            while (true) {
                try {

                    double sumFocus = 0;
                    double[] arrayFocus = new double[2];

                    for (int i = 0; i < 2; i++) {
                        arrayFocus[i] = scan.nextDouble();
                    }
                    for (double num : arrayFocus) {
                        sumFocus = sumFocus + num;
                    }
                }

                catch (InputMismatchException e) {

                    System.out.println("Invalid input");
                    scan.nextLine();
                }

            }
        }
    }

}

标签: javaif-statement

解决方案


在您的 catch 语句中,您使用scan.nextLine(),但您应该使用scan.nextDouble(),就像您在主循环中所做的那样。也就是说,您希望接受双精度数作为输入。此外,您的 try/catch 语法似乎是错误的。您想捕获可能由用户输入引起的错误;因此,您需要在用户在try块中输入内容的位置添加代码。见下文:

try {
            double tempValue = scan.nextDouble();

            //this loop will keep spinning, until tempValue is valid
            while (tempValue > 100d || tempValue < 0d) {
                System.out.println("Invalid. Try again. Range is 0 - 100");
                tempValue = scan.nextDouble();
            }
            //this loop will spin forever (There is nothing to terminate it)
            while (true) {

                double sumFocus = 0;
                double[] arrayFocus = new double[2];

                for (int i = 0; i < 2; i++) {
                    arrayFocus[i] = scan.nextDouble();
                }
                for (double num : arrayFocus) {
                    sumFocus = sumFocus + num;
                }
            }
        }
        catch (InputMismatchException e) {

            System.out.println("Invalid input");
            scan.nextDouble(); //this doesn't do anything. The value is not assigned to any variable
        }

使用上面的代码,当用户输入一个无效字符时,例如:一个字母,程序就会停止。为什么?因为catch在无限循环之外,这使得你的程序计数器移动到循环之外,并且当catch语句中的代码被执行时,你的程序也会在到达结束时终止。

你可以做的是这样的:

static double validValueWithRange(Double min, Double max){
   Scanner s = new Scanner(System.in);
   System.out.println("Enter a valid value for a 'double': ");
   double value;
   try {
       value = s.nextDouble();
       if(value > max || value < min){
         System.out.println("Invalid. Try again. Range is 0 - 100");
         return validValueWithRange(min, max);
       }
    }
    catch (InputMismatchException e) {
        System.out.println("Invalid input. Try again.");
        return validValueWithRange(min, max);
    }

    return value;
}

static double validValue(){
    Scanner s = new Scanner(System.in);
    System.out.println("Enter a valid value for a 'double': ");
    double value;
    try {
        value = s.nextDouble();
    }
    catch (InputMismatchException e) {
        System.out.println("Invalid input. Try again.");
        return validValue();
    }

    return value;
}

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

    double tempValue = validValueWithRange(0d, 100d);

    while (true) {
        System.out.println("Now entering endless while loop");
        double sumFocus = 0;
        double[] arrayFocus = new double[2];

        for (int i = 0; i < 2; i++) {
            arrayFocus[i] = validValue();
        }
        for (double num : arrayFocus) {
            sumFocus = sumFocus + num;
        }
    }
}

推荐阅读