首页 文章

使用If语句中的临时值继续尝试捕获

提问于
浏览
2

我需要输入数组的值是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();
                }

            }
        }
    }

}

2 回答

  • -1

    您只需将变量的初始化放在try和catch块中,如下所示:

    try {
        double tempValue = scan.nextDouble();
    } catch (InputMismatchException ex) {
        System.out.println("Please enter a number.");
    }
    
  • 1

    在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;
            }
        }
    }
    

相关问题