2015-02-23 90 views
-1

我似乎與我的套牌項目出現錯誤。我想打印出的卡片洗牌甲板和我已經有了幫助,但這個錯誤現在已經停止從我的進展Java ArrayIndexOutOfBoundsException當試圖測試

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 51 
 
\t at Pack.<init>(Pack.java:14) 
 
\t at PackTester.main(PackTester.java:14) 
 
Java Result: 1 
 
BUILD SUCCESSFUL (total time: 2 seconds)

public class Pack { 

private PlayingCard[] deck; // An array of 52 cards, representing the deck. 
private int cardsUsed; // How many cards have been dealt from the deck. 

/** 
* Creating an unshuffled deck of cards 
*/ 
public Pack() { 
    deck = new PlayingCard[51]; //Creates an array of 52 playing cards 
    int cardCt = 0; // How many cards have been created so far. 
    for (int suit = 0; suit <= 3; suit++) { //If a suit is complete, move to the next suit 
     for (int rank = 1; rank <= 14; rank++) { //Builds a complete suit 
     deck[51] = new PlayingCard(rank, suit); 
     cardCt++; //Adds one to the card count 
     } 
    } 
    cardCt = 0; 
} 

/** 
* Shuffling a deck of cards 
*/ 
public void shuffle() { 
     // Put all the used cards back into the deck, and shuffle it into 
     // a random order. 
    for (int i = 51; i > 0; i--) { 
     int rand = (int)(Math.random()*(i+1)); 
     PlayingCard temp = deck[i]; 
     deck[i] = deck[rand]; 
     deck[rand] = temp; 
    } 
    cardsUsed = 0; 
} 

public @Override String toString() { 

String deckStr = ""; 

for (int i=0; i<52; i++) { 
    deckStr = deckStr + deck[i].toString() + " "; 
} 

return deckStr; 
} 
} // end class Pack 

這裏是測試儀類。

public class PackTester { 

public static void main(String[] args) 
{ 
    Pack myPack = new Pack(); 
    myPack.shuffle(); 
    System.out.println(myPack.toString()); 
} 
} 

我只是不知道從哪裏去,所以任何幫助,將不勝感激。

+0

考慮使用列表並讓您的PlayingCard類實現Comparable接口,然後您可以通過調用Collections.shuffle(myCardList) – 2015-02-23 11:54:48

回答

0

爲了創造的52張一副撲克牌,你需要:

deck = new PlayingCard[52]; 

此外,這是毫無意義的循環總是指定卡到第51位:

deck[51] = new PlayingCard(rank, suit); 

這會更有意義:

public Pack() { 
    deck = new PlayingCard[52]; //Creates an array of 52 playing cards 
    int cardCt = 0; // How many cards have been created so far. 
    for (int suit = 0; suit <= 3; suit++) { //If a suit is complete, move to the next suit 
     for (int rank = 1; rank < 14; rank++) { //Builds a complete suit 
     deck[cardCt] = new PlayingCard(rank, suit); 
     cardCt++; //Adds one to the card count 
     } 
    } 
} 

另請注意,應該有13個等級,而不是14個。

+0

輕鬆地洗牌。仍然收到相同的錯誤 – 2015-02-23 10:55:44

+0

@BenParry您無法獲取java。 lang.ArrayIndexOutOfBoundsException:如果將數組的長度更改爲52,則會出現「51」錯誤。要麼您沒有更改它,要麼得到不同的錯誤。 – Eran 2015-02-23 10:57:43

+0

雖然,我已經改變了51到52,我得到了同樣的錯誤,但是現在的數字是52 – 2015-02-23 11:01:13

0

你定義的數組爲:

deck = new PlayingCard[51]; 

你嘗試添加元素,如:

deck[51] = new PlayingCard(rank, suit); 

您試圖在數組來設置51度元件。

for (int i = 51; i > 0; i--) { //either start with 50 or define array as PlayingCard[52] 
int rand = (int)(Math.random()*(i+1)); 
PlayingCard temp = deck[i]; 

記住索引的陣列從0開始直到直到n -1個,所以需要52次元件(52張牌,即,第52將由索引51可以訪問):

在洗牌方法

另外,當你定義數組時,將數組容量從51增加到52。