0

コードの実行に問題があり、エラーの原因や理由を正確に特定することができません。可能であれば、誰かが見てフィードバックを提供してくれるかもしれません。

エラーメッセージ:

51
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
3 of Clubs, Diamonds, Hearts, Spades
    at javacards.Card.toString(Card.java:15)
    at javacards.CardRun.main(CardRun.java:15)

CardRun クラス:

public class CardRun {

public static void main(String[] args)
{
    Deck deck = new Deck();
    Card C;

    System.out.println(deck.getTotalCards());

    while(deck.getTotalCards() != 0)
    {
        C = deck.drawFromDeck();
        System.out.println(C.toString());
    }
}

カードクラス

public class Card {
    private int card, suit;
    private static String[] suits = {"Clubs, Diamonds, Hearts, Spades"};
    private static String[] cards = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};

    Card(int suit, int card)
    {
        this.card = card;
        this.suit = suit;
    }

    public @Override String toString()
    {
        return cards[card] + " of " + suits[suit];
    }

    public int getCard()
    {
        return card;
    }

    public int getSuit()
    {
        return suit;
    }
}

デッキクラス

public class Deck {

private Card[]cards;
int i;

Deck()
{
    i = 51; 
    cards = new Card[52];
    int x = 0;
    for(int a=0; a<=3; a++)
    {
        for(int b=0; b<=12; b++)
        {
            cards[x] = new Card(a,b);
            x++;
        }
    }
}

public Card drawFromDeck()
{
    Random generator = new Random();
    int index = 0;

    index = generator.nextInt(i);
    Card temp = cards[index];
    cards[index] = cards[i];
    cards[i] = null;
    i--;
    return temp;
}

public int getTotalCards()
{
    return i;
}
}
4

1 に答える 1

9

この配列:

private static String[] suits = {"Clubs, Diamonds, Hearts, Spades"};

アイテムが 1 つだけ含まれています - おそらく次のことを意味していました:

private static String[] suits = {"Clubs", "Diamonds", "Hearts", "Spades"};
于 2013-03-10T17:52:56.370 に答える