我正在尝试使用一个数组来发牌,然后按花色对它进行排序。该程序处理所有 13 张牌,但随后出现错误,
"Exception in thread "main"
java.lang.ArrayIndexOutOfBoundsException: 13
at DeckStuff.main(DeckStuff.java:29)"
如有任何帮助,我们将不胜感激。
public static void main(String[] args) {
Card.shuffleDeck();
Card.dumpCards();
Card[] bridgeHand = new Card[13];
for (int i = 0; i < bridgeHand.length; i++) {
bridgeHand[i] = Card.dealCard();
}
for (int i = 0; i < bridgeHand.length; i++) {
System.out.print(bridgeHand[i]);
}
System.out.println();
Card.sortHand(bridgeHand);
String[] suit = {"Spades", "Hearts", "Diamonds", "Clubs"};
int j = 0;
for(int i = 0; i < 4; i++) {
String s = suit[i].substring(0,1);
System.out.print("\n" + s + ": ");
while (bridgeHand[j].getCardSuitName().equals(suit[i])) {
System.out.print(bridgeHand[j]);
j++;
}
}
}
}
最佳答案
java.lang.ArrayIndexOutOfBoundsException: 13
这意味着您正在越界访问数组。您给它的索引值为 13。我什至还没有读过您的代码,但我猜这是一个差一错误。
...阅读...
是的。你不断增加 j。错误消息还会为您提供行号。
在 DeckStuff.main(DeckStuff.java:29)"
这意味着错误在文件 DeckStuff.java 的第 29 行
关于java - 线程 "main"java.lang.ArrayIndexOutOfBoundsException : 13 中的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16203680/