2015-02-23 69 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。