洗牌的卡片组

时间:2018-02-13 19:35:53

标签: java

我无法构建我的程序的随机播放部分来洗牌。我只需要帮助构建shuffle部分。我得到的错误是它无法找到"符号:varible suit"

package playingcard;

import java.util.Arrays;
import java.util.Random;

public class Pack {

    PlayingCard[] cards = new PlayingCard[52];
    public int counter = 0;

    /**
     * Constructs a pack of 52 cards. Sorted by suit Clubs, Diamonds, Hearts,
     * Spades. Sorted ascending.
     */
    public void Pack() {

        // Intialising array
        {
            for (int i = 0; i < PlayingCard.Suit.values().length; i++) {
                for (int j = 0; j < PlayingCard.Rank.values().length; j++) {
                    PlayingCard.Suit suit = PlayingCard.Suit.values()[i];
                    PlayingCard.Rank rank = PlayingCard.Rank.values()[j];
                    cards[counter] = new PlayingCard(suit, rank);
                    counter++;
                }
            }
        }

    }

    /**
     * Shuffles cards in pack.
     */
    public void shuffle() {

        for (int i = 0; i < 52; i++) {
           Random random = new Random();
            int rand = random.nextInt(50)+1;
            cards[counter] = PlayingCard(suit, rank);

        }
    }

    /**
     * @return string representation of 52 card pack.
     */
    @Override
    public String toString() {
        return null;
    }
}

1 个答案:

答案 0 :(得分:0)

您尝试做的是尝试让多种方法访问同一种方法 变量。这样做的方法是在类声明后立即声明该变量, 就像你宣布你的卡变量一样。这样所有方法都可以访问和你 不会得到那个错误。这是固定代码。

public class  Pack{

PlayingCard[] cards = new PlayingCard[52];
PlayingCard.Suit suit;
PlayingCard.Rank rank;
public int counter = 0;

/**
 * Constructs a pack of 52 cards. Sorted by suit Clubs, Diamonds, Hearts,
 * Spades. Sorted ascending.
 */
public void Pack() {

    // Intialising array
    {
        for (int i = 0; i < PlayingCard.Suit.values().length; i++) {
            for (int j = 0; j < PlayingCard.Rank.values().length; j++) {
                suit = PlayingCard.Suit.values()[i];
                rank = PlayingCard.Rank.values()[j];
                cards[counter] = new PlayingCard(suit, rank);
                counter++;
            }
        }
    }

}

/**
 * Shuffles cards in pack.
 */
public void shuffle() {

    for (int i = 0; i < 52; i++) {
       Random random = new Random();
        int rand = random.nextInt(50)+1;
        cards[counter] = PlayingCard(suit, rank);

    }
}

/**
 * @return string representation of 52 card pack.
 */
@Override
public String toString() {
    return null;
}

}