2014-09-30 95 views
0

我遇到了try/catch塊的問題。現在,我的代碼在某種程度上起作用,但是,例如,當輸入一個無效的數字(「a」)時,我打印出該程序(「請輸入一個有效的數字:」);但是,我有一個方法可以在輸入的索引處獲取對象,並說明局部變量可能尚未初始化。Try/Catch變量無法初始化

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); 
+0

錯誤指定哪行的哪個變量? – David 2014-09-30 18:27:57

+0

'ArrayIndexOutOfBoundsException'從哪裏來? – Tom 2014-09-30 18:30:42

+0

Card card = Deck.randomizedCards.get(firstNum); Card card1 = Deck.randomizedCards.get(secondNum); Card card2 = Deck.randomizedCards.get(thirdNum);這些參數中的變量未被初始化。 – 2014-09-30 18:31:33

回答

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): "); 
2

由於在try語句中的分配可能不會發生你有你的變量firstNumsecondNumthirdNum留下潛在unititialized。

您可以通過分配默認值或將邏輯移入相同的try語句來規避該情況。

0

編譯器(正確)警告您,例如,如果行secondNum = scan.nextInt();引發異常,則變量(如thirdNum)可能未被初始化。

爲了規避這個問題,您應該爲所有變量指定默認值,或者在拋出異常時使該方法返回。

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); 
+0

我試着運行代碼,並且我得到了一個無限循環的println語句詢問一個數字。 – 2014-09-30 18:37:35

+0

Ofcourse如果用while(true)和no break語句運行循環。檢查編輯。 – StackFlowed 2014-09-30 18:38:21

+0

我看到了,我在break語句中添加了,但是我仍然遇到了無限循環。我試圖讓用戶不斷輸入顯示卡的索引。 – 2014-09-30 18:46:09