不显示卡片

Not displaying cards

我正在创建一副纸牌实例,构建时应该显示所有 52 张纸牌,然后它应该洗牌并发 5 张。在此之后,菜单应该提示要发另外 5 张纸牌,或者提示牌组重新洗牌,将所有抽取的牌重新加入,或退出应用程序。但是,当我 运行 应用程序时,它只对所有 52 张牌和发的每张牌显示 null。在我与我的导师交谈之前,该应用程序运行良好并且几乎满足所有规范,而我现在所拥有的是我在谈话期间/之后得到的结果。我不确定我做错了什么,真的可以使用帮助。只是为了澄清,我将 post 需求,然后是代码。感谢您的帮助。

At startup, it constructs a Deck of Cards
52 distinct cards running from Ace - King, of suit Heart, Club, Diamond, or Spade
Shuffle the cards!!
For proof, print out all 52 cards to the console in a useful, readable way.
Then, deal the top five cards to the console (meaning print them to the console)
After all of this, allow the user choose between dealing the next five cards, reshuffling the deck, or quitting the application. 
If the user chooses to deal the next 5 cards, do so based on which cards have not yet been dealt. DO NOT RESHUFFLE.
If the user chooses to reshuffle, simply repeat the process of shuffling, printing the deck, and dealing the top five cards.
If the user chooses to quit the application, simply end the app.

我正在练习封装的代码,所以我将 post 每个 class 像在 eclipse 中一样分解。

Driver

public class Driver {

    public static void main(String[] args) throws IOException {

        DeckRun.run();

        }

    }

DeckRun

    public class DeckRun {

    static Card[] d1 = new Card[52];

    public static void run() throws IOException {

        printDeck();
        System.out.println("");
        Deal.dealCards(d1);
        System.out.println("");
        menu();

    }

    public static void printDeck() {

    for (Card c : d1) {
        System.out.println(c);
    }

    }

    public static void menu() throws IOException{

        BufferedReader readRacer = new BufferedReader(new InputStreamReader(System.in));

        int menu = 0;

        do {

        System.out.println("Press 1 to be dealt 5 random cards.");
        System.out.println("Press 2 to shuffle all the cards back into the deck.");
        System.out.println("Press 3 to quit the application.");

        String input = readRacer.readLine();
        menu = Integer.parseInt(input);

        switch (menu) {

        case 1: Deal.dealCards(d1);
            break;
        case 2: System.out.println("The deck has been shuffled.");
                Deck[] d1 = new Deck[52];
            break;
        case 3: System.out.println("I'm not bad, I'm just drawn that way.");
            break;
        }
        } while (menu != 3);
    }

}

卡片

public class Card {

    private Rank rank; // Variable to assign a card its rank.
    private Suit suit; // Variable to assign a card its suit.

    public Card (Rank rank, Suit suit) { // Constructor to build a card.
        this.rank = rank;
        this.suit = suit;
    }

    public Rank getRank() { // Retrieves the card's rank from the enum Rank.
        return rank;
    }

    public Suit getSuit() { // Retrieves the card's suit from the enum Suit.
        return suit;
    }

    @Override
    public String toString() { //
        return rank + " OF " + suit;
    }

}

甲板

public class Deck {

    private Card[] cards;

    public Deck() {

        int numberOfRanks = 13;
        int numberOfSuits = 4;
        int numberOfCards = numberOfRanks * numberOfSuits;

        Rank[] rank = Rank.values();
        Suit[] suit = Suit.values();

        cards = new Card[numberOfCards];

        for (int i = 0; i < numberOfRanks; i++) {
            for (int j = 0; j < numberOfSuits; j++) {

                cards[j * numberOfRanks + i] = new Card(rank[i], suit[j]);
            }
        }
    }

    public void shuffleCards() {

        Random rand = new Random();

        for (int c = rand.nextInt(6) + 5; c > 0; c--) {
            for (int i = cards.length - 1; i > 0; i--) {

                int index = rand.nextInt(i + 1);
                Card card = cards[index];
                cards[index] = cards[i];
                cards[i] = card;
            }
        }
    }
}

成交

public class Deal {

    private static int counter = 0;

    public static void dealCards(Card[] d1) {

        for (int i = 0; i < 5; i++) {
            counter++;
            System.out.println(d1[counter]);
            if (counter == 50) {
                System.out.println("Almost all cards have been used, please reshuffle.");
            }
        }
    }
}

排名(枚举)

public enum Rank { ACE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT,
NINE, TEN, JACK, QUEEN, KING, }

套装(枚举)

public enum Suit { CLUBS, DIAMONDS, HEARTS, SPADES, }

不太确定问题出在哪里,但我今天所做的改变是在意识到我需要一副牌的实例之后,我的教授帮助我理解了 [=38] 中的一副牌的方法=] Deck,我需要一个构造函数。我相信我走的路是正确的,但我完全被难住了。感谢任何帮助,如果你能解释我的错误,那么我可以学习和进步更多,谢谢。

main中你调用了DeckRun.run(),我们来看看你对这个方法的定义。它调用 printDeck 遍历卡片数组 (d1),该数组从未被填充。这就是为什么您会收到一堆 NULL 的原因。

你的 DeckRun 应该有一个 Deck 的实例而不是卡片数组

 public class DeckRun {
    private Deck deck = new Deck();
    public static void run() throws IOException {
        printDeck();
        System.out.println("");
        Deal.dealCards(deck.getCards());
        System.out.println("");
        menu();
    }
    public static void printDeck() {
       for (Card c : deck.getCards()) {
           System.out.println(c);
       }
    }

    public static void menu() throws IOException{
        BufferedReader readRacer = new BufferedReader(new InputStreamReader(System.in));
        int menu = 0;
        do {
           System.out.println("Press 1 to be dealt 5 random cards.");
           System.out.println("Press 2 to shuffle all the cards back into the deck.");
           System.out.println("Press 3 to quit the application.");
           String input = readRacer.readLine();
           menu = Integer.parseInt(input);
           switch (menu) {
             case 1: 
                Deal.dealCards(deck.getCards());
                break;
             case 2: 
                System.out.println("The deck has been shuffled.");
                deck.shuffleCards();
                break;
             case 3: 
                System.out.println("I'm not bad, I'm just drawn that way.");
                break;
           }
        } while (menu != 3);
    }
}

您还需要为 Deck

中的卡片添加 getter

所以你的问题出在这个循环

      for (int i = 0; i < numberOfRanks; i++) {
        for (int j = 0; j < numberOfSuits; j++) {

            cards[j * numberOfRanks + i] = new Card(rank[i], suit[j]);
        }
    }

J*numOfRanks +i 基本上是说 0*4 = 0+0 = 0

等等,造成问题。为了使这个更简单,我会推荐一个二维数组,为西装制作 4 列,为队伍制作 13 行,然后类似的循环将完美地工作。如果这有帮助,请告诉我!

好的伙计们。答案很简单。我删除了交易 class,并将方法移到了牌组 class。我将计数器初始化为私有的 class 级别的 int,这解决了重复问题。下面我将post完成的代码,大家可以看看。

DeckRun Class

public class DeckRun {

    static Deck d1 = new Deck();

    public static void run() throws IOException {

        printDeck();
        System.out.println("");
        d1.dealCards();
        System.out.println("");
        menu();

    }

    public static void printDeck() {

        System.out.println(d1.toString());
    }


    public static void menu() throws IOException{

        BufferedReader readRacer = new BufferedReader(new InputStreamReader(System.in));

        int menu = 0;

        do {

        System.out.println("Press 1 to be dealt 5 random cards.");
        System.out.println("Press 2 to shuffle all the cards back into the deck.");
        System.out.println("Press 3 to quit the application.");

        String input = readRacer.readLine();
        menu = Integer.parseInt(input);

        switch (menu) {

        case 1: d1.dealCards();
        System.out.println("");
            break;
        case 2: System.out.println("The deck has been shuffled.");
                    d1.shuffleCards();
                    System.out.println("");
            break;
        case 3: System.out.println("I'm not bad, I'm just drawn that way.");
            break;
        }
        } while (menu != 3);
    }

}

甲板Class

public class Deck {

    private Card[] cards;

    public Deck() {

        int numberOfRanks = 13;
        int numberOfSuits = 4;
        int numberOfCards = numberOfRanks * numberOfSuits;

        Rank[] rank = Rank.values();
        Suit[] suit = Suit.values();

        cards = new Card[numberOfCards];

        for (int i = 0; i < numberOfRanks; i++) {
            for (int j = 0; j < numberOfSuits; j++) {

                cards[i * numberOfSuits + j] = new Card(rank[i], suit[j]);
            }
        }
    }

    public void shuffleCards() {

        Random rand = new Random();
        counter = 0;

        for (int c = rand.nextInt(6) + 5; c > 0; c--) {
            for (int i = cards.length - 1; i > 0; i--) {

                int index = rand.nextInt(i + 1);
                Card card = cards[index];
                cards[index] = cards[i];
                cards[i] = card;
            }
        }
    }

    private int counter = 0;

    public void dealCards() {

        try{
        for (int i = 0; i < 5; i++) {
            counter++;
            System.out.println(cards[counter]);
            if (counter == 50) {
                System.out.println("Almost all cards have been used, please reshuffle.");
                // Either return 1 card or an array of 5 cards.
            }
        } 
        } catch (ArrayIndexOutOfBoundsException aioobe){
            System.out.println("Caught an ArrayIndexOutOfBoundsException. Reshuffling deck.");
            shuffleCards();
        }
    }

    @Override
    public String toString() {

        String deckOfCards = "";

        for (Card c : cards) {

            deckOfCards += c.toString() + "\n";

        }

        return deckOfCards;

    }
}

这就是我必须解决的全部问题,但非常感谢您的帮助。谢谢大家。