我遇到了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);
錯誤指定哪行的哪個變量? – David 2014-09-30 18:27:57
'ArrayIndexOutOfBoundsException'從哪裏來? – Tom 2014-09-30 18:30:42
Card card = Deck.randomizedCards.get(firstNum); Card card1 = Deck.randomizedCards.get(secondNum); Card card2 = Deck.randomizedCards.get(thirdNum);這些參數中的變量未被初始化。 – 2014-09-30 18:31:33