我正在嘗試編寫一個代碼,其中我構造了一個52張牌堆,然後將這些牌交給n個玩家(對於某些玩家可能有額外的牌)。獲勝者是擁有黑桃王牌的人。數組越界java
這裏是我的程序:
public class CardGame {
public static void main(String[] args) {
System.out.println("Enter the number of players");
int numofPlayers = Integer.parseInt(args[0]);
CardPile gameDeck = CardPile.makeFullDeck();
CardPile [] players = new CardPile[numofPlayers];
for (int i=0;i<numofPlayers;i++) {
int numofnum = i%numofPlayers;
players[i] = new CardPile();
}
for (int i=0;i<52;i++) {
int numofnum =i%numofPlayers;
CardPile curPlayer = players[i%numofPlayers];
Card nextCard = gameDeck.get(i);
players[numofnum].addToBottom(nextCard);
}
for (int i=1;i<numofPlayers;i++) {
if (players[i].find(Suit.SPADES, Value.ACE) != -1) {
System.out.println("Player" + i + "has won!");
}
}
}
}
我不斷收到出界失誤。我在這個程序中調用的方法寫得很好,所以問題應該來自這個代碼。誰能幫忙?
編輯:這是錯誤,我得到
java.lang.ArrayIndexOutOfBoundsException: 0
at CardGame.main(CardGame.java:5)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
>
謝謝!
究竟在哪一行你會得到'ArrayOutOfBoundsException'?請同時發佈錯誤的完整堆棧跟蹤。 – Blip
你能指出'CardGame'類的第5行嗎? – Blip
這是命令行參數吧? – Jayzpeer