繁体   English   中英

在 do-while 循环中尝试/捕获以检查用户输入(数组) - 错误的 boolean 初始化和 position

[英]Try / catch in a do-while loop to check user input (array) - wrong boolean initialization and position

我是 Java 初学者,我正在学习管理异常和 2D arrays。 我正在对 plot 做一个 2D 数组中不同车型的销售结果的练习。

我通过使用 try catch 或抛出自定义异常来混合处理异常的方式。 在自定义异常上,我有一个无法解决的问题。

在卖家 class 中:

  • 在第 16 行,IDE 告诉我变量“正确”初始化为 false 是多余的冗余初始化
  • 在第 26 行,IDE 告诉我 while 条件始终为假。 do while 循环的最终条件错误

我想在内循环中声明 boolean 正确,以检查输入是否正确(整数),然后在尝试结束时将值更改为 true,以防 parseInt 成功。 By reading the comments from the IDE I understand that the boolean correct is never changed to true even if I select an integer but I do not understand why.

import java.util.Scanner;
public class Sellers {
    public static void main(String[] args) throws NumberArrayException {
        Scanner myObj = new Scanner(System.in);

        int nbrOfSellers = CheckInput.control("Enter the number of sellers = ");
        int nbrOfModels = CheckInput.control("Enter the number of models = ");
        int[][] sales = new int[nbrOfSellers][nbrOfModels];

        String[] nameOfSellers = Characters.construction("seller", nbrOfSellers);
        String[] nameOfModels = Characters.construction("model", nbrOfSellers);


        for(int i = 0; i < nbrOfSellers; i++) {
            for(int j = 0; j < nbrOfModels; j++) {
                boolean correct = false;
                System.out.print("Enter the sales for the seller " + nameOfSellers[i] + " for the model " + nameOfModels[i] + " = ");
                do {
                    try {
                        String input = myObj.nextLine();
                        sales[i][j] = Integer.parseInt(input);
                        correct = true;
                    } catch (NumberFormatException e) {
                        throw new NumberArrayException();
                    }
                }while (!correct) ;
            }
        }
        for(int i = 0; i < nbrOfSellers; i++) {
            for(int j = 0; j < nbrOfModels; j++) {
                System.out.print(sales[i][j] + " ");
            }
            System.out.println();
        }
    }
}

import java.util.Scanner;
public class Characters {
    public static String[] construction(String character, int maxSize) {
        Scanner myObj = new Scanner(System.in);
        String[] myArray = new String[maxSize];
        for(int i = 0; i < maxSize; i++) {
            System.out.print("Enter the name of the " + character + " ");
            myArray[i] = myObj.nextLine();
        }
        return myArray;
    }
}

import java.util.Scanner;
public class CheckInput {
    public static int control(String message) {
        boolean correct = false;
        int number = -1;
        Scanner myObj = new Scanner(System.in);
        do {
            try {
                System.out.print(message);
                String input = myObj.nextLine();
                number = Integer.parseInt(input);
                correct = true;
            } catch(NumberFormatException e) {
                System.out.println("Enter a number");
            }
        }while(!correct);

        return number;
    }
}

public class NumberArrayException extends RuntimeException {
    public NumberArrayException() {
        super();
    }
}

这个do-while-loop有两种退出方式: correct或抛出异常。

从技术上讲,只能在语句correct = true上面的两行中引发异常。 由于您在循环中抛出NumberArrayException ,因此它将因此而退出,或者将correct设置为true

当我们到达while (!correct)时, correct的总是正确的,因为它被设置了。 否则我们将退出异常。 因此,这个循环永远不会有第二次迭代。

要修复它,您可以避免在无法解析提供的输入时引发异常。 然后循环将让用户输入另一个 integer。

do {
    try {
        String input = myObj.nextLine();
        sales[i][j] = Integer.parseInt(input);
        correct = true;
    } catch (NumberFormatException e) {
        // output error message for the user here
    }
} while (!correct);

您的代码非常好并且结构良好。 实际上,您应该比较CheckInput.control的来源,看看为什么它没有提到的警告:您将局部变量初始化为correct的,并且在输入无效的情况下不会引发异常,从而让用户更正它. 如果您像在main方法中一样从do-while循环内部抛出异常,则永远不会使用correctfalse值。

在初始化 2D sales数组时,您应该通过重用现有的CheckInput.control来解决这些警告。 在创建nameOfModels时还有一个错字—— nbrOfModels应该作为参数传递。

String[] nameOfSellers = Characters.construction("seller", nbrOfSellers);
String[] nameOfModels = Characters.construction("model", nbrOfModels);


for(int i = 0; i < nbrOfSellers; i++) {
    for(int j = 0; j < nbrOfModels; j++) {
        sales[i][j] = CheckInput.control("Enter the sales for the seller " + nameOfSellers[i] + " for the model " + nameOfModels[i] + " = ");
    }
}

当您抛出异常时,它会退出 function 并抛出异常。 因此,不要在 catch 块中抛出异常,只需打印和错误消息。 这样用户就可以理解错误并再次输入。

 do {
     try {
          String input = myObj.nextLine();
          sales[i][j] = Integer.parseInt(input);
          correct = true;
          } catch (NumberFormatException e) {
                System.out.println("Wrong input format. Please enter a number");
          }
 }while (!correct) ;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM