Try / Catch变量无法初始化

时间:2014-09-30 18:25:27

标签: java loops variables try-catch java.util.scanner

我的try / catch块有问题。现在,我的代码有点工作,但是,当我输入无效数字(“a”)时,我打印出程序,(“请输入有效数字:”);但是,我有一个方法可以在输入的索引处获取对象,并且声明局部变量可能尚未初始化。

do {
        int firstNum;
        int secondNum;
        int thirdNum;
        System.out
                .println("Please enter three of the numbers you see on the left of the shape:");
        try {

            firstNum = scan.nextInt();
            secondNum = scan.nextInt();
            thirdNum = scan.nextInt();

            Card card = Deck.randomizedCards.get(firstNum);
            Card card1 = Deck.randomizedCards.get(secondNum);
            Card card2 = Deck.randomizedCards.get(thirdNum);
            if (Deck.isSet(card, card1, card2) == true) {
                JOptionPane.showMessageDialog(panel, "You found a set!");
                Deck.completedSets.add(card);
                Deck.completedSets.add(card1);
                Deck.completedSets.add(card2);

            } else {
                JOptionPane.showMessageDialog(panel,
                        "You didn't find a set");
            }
             break;
        } catch (InputMismatchException name) {
            System.out
                    .println("You have entered an invalid number, please enter a number:");

        } catch (ArrayIndexOutOfBoundsException name) {
            System.out
                    .println("You have entered an invalid number, please enter a number:");

        }

    } while (true);

4 个答案:

答案 0 :(得分:2)

由于try语句中的分配可能不会发生,因此您的变量firstNumsecondNumthirdNum可能会被单元化。

您可以通过分配默认值或将逻辑移动到相同的try语句来规避这一点。

答案 1 :(得分:0)

编译器(正确地)警告您,例如,当thirdNum行抛出异常时,secondNum = scan.nextInt();等变量可能尚未初始化。

为避免此问题,您应该为所有变量指定默认值,或者在抛出异常时返回该方法。

答案 2 :(得分:0)

int firstNum;
int secondNum;
int thirdNum;
do {
    System.out.println("Please enter three of the numbers you see on the left of the shape:");
    try {
       firstNum = scan.nextInt();
       secondNum = scan.nextInt();
       thirdNum = scan.nextInt();
       Card card = Deck.randomizedCards.get(firstNum);
       Card card1 = Deck.randomizedCards.get(secondNum);
       Card card2 = Deck.randomizedCards.get(thirdNum);
       if (Deck.isSet(card, card1, card2)) {
           JOptionPane.showMessageDialog(panel, "You found a set!");
           Deck.completedSets.add(card);
           Deck.completedSets.add(card1);
           Deck.completedSets.add(card2);
           break;  
        } else {
           JOptionPane.showMessageDialog(panel, "You didn't find a set");
        }
    } catch (InputMismatchException name) {
        System.out
                .println("You have entered an invalid number, please enter a number (Range 1-11): ");

    } catch (ArrayIndexOutOfBoundsException name) {
        System.out
                .println("You have entered an invalid number, please enter a number(Range 1-11): ");
    }
} while (true);

答案 3 :(得分:0)

主要问题在于此代码片段

try{
   firstNum= scan.nextInt();
.
.
.
catch (InputMismatchException name) {
    System.out.println("You have entered an invalid number, please enter a number (Range 1-11): ");

}

当抛出输入异常时,有问题的输入留在流上,所以当执行再次到达scan.nextInt()时,它会得到相同的错误输入,因此抛出相同的异常,依此类推。将catch块更改为:

 catch (InputMismatchException name) {
  String malformedGarbage = scan.Next();//processes the bad input so the scanner can move on.  
  System.out.println("You have entered an invalid number, please enter a number (Range 1-11): ");