似乎无法打印出充满对象/枚举的arraylist

时间:2013-12-10 05:30:03

标签: java arrays printing arraylist enums

我正在尝试创建一个卡片游戏高尔夫球,以获得乐趣并练习我的Java技能。我正在尝试使用枚举来获取我的卡片值和套件。这些值保存在卡类中的名为Card的构造函数中。

我遇到的问题是打印我的所有个人卡片的arraylist Deck。可以在DeckOfCards类中找到此方法。我想知道我的程序是否正在创建一副完整的卡片。在此先感谢您的帮助!

DeckOfCards Class

package finalProjectGolf;
    // Deck class represent a deck of playing cards.

import java.util.ArrayList;
import java.util.Collections;

public class DeckOfCards {


    ArrayList<Card> deck = new ArrayList<Card>(); //array of Card objects

    private int currentCard; // index of next Card to be dealt (0-51)
    private static int numDecks = 1;

    public int getCurrentCard() {
        return currentCard;
    }

    public void setCurrentCard(int currentCard) {
        this.currentCard = currentCard;
    }

    private static final int NUMBER_OF_CARDS = 52 * numDecks; //constant # of Cards


    //constructor fill deck of Cards
    public DeckOfCards(){

        currentCard = 0; //set currentCard so first Card dealt is deck[0]

        //Card Index
            int c = 0;
        //for each deck
                for (int d = 0; d < numDecks; d++){
                    //for each suit
                    for (int s = 0; s < 4; s++){
                        // for each number
                        for (int n = 1; n <= 13; n++){
                            //add a new card to the deck
                            deck.add(new Card(CardValue.values()[n],Suit.values()[s]));   //when using Enums java makes arrays automatically and you can use them by .values()
                            c++;    
                        }}}//end for loop
    }//end DeckOfCards constructor

    //shuffle deck of Cards with one-pass algorithm
    public void shuffle() {
        Collections.shuffle(deck);
    }

    public int points(){
        int value = deck.get(currentCard).getCardValue().getCardValue();
        return value;
    }

    //deal one Card
    public Card dealCard(int currentCard) {

        //determine whether Cards remain to be dealt
        if( currentCard < deck.size() ){
            return deck.get(currentCard); //return current Card in array
        }
        else
            return null; // return null to indicate that all Cards were dealt
    }//end method dealCard


    public void printDeck(){
        {
            currentCard = 0; //set currentCard so first Card dealt is deck[0]

            //Card Index
                int c = 0;
            //for each deck
                    for (int d = 0; d < numDecks; d++){
                        //for each suit
                        for (int s = 0; s < 4; s++){
                            // for each number
                            for (int n = 1; n <= 13; n++){
                                //add a new card to the deck
                                System.out.printf("");   //when using Enums java makes arrays automatically and you can use them by .values()
                                c++;    
                            }}}//end for loop
        }//end DeckOfCards constructor

    }
}// end class DeckOfCards

卡类

package finalProjectGolf;


public class Card
{
      private Suit suit;
      private CardValue cardValue;


      public Card (CardValue cardValue, Suit suit) //constructor of Card, holds Card value and it's suit
      {
        this.cardValue = cardValue;
        this.suit = suit;

      }

      public Suit getSuit()
      {
        return suit;
      }

      public void setSuit(Suit suit)
      {
        this.suit = suit;
      }

      public CardValue getCardValue()
      {
        return cardValue;
      }

      public void setCardValue(CardValue cardValue)
      {
        this.cardValue = cardValue;
      }

      public String toString(){
            return  cardValue + " of " + suit;
        }// end method toString


    }

CardValue Class

package finalProjectGolf;


    public enum CardValue
    {
      ACE(1),
      TWO(2),
      THREE(3),
      FOUR(4),
      FIVE(5),
      SIX(6),
      SEVEN(7),
      EIGHT(8),
      NINE(9),
      TEN(10),
      JACK(11),
      QUEEN(12),
      KING(13);

      private int cardValue;

      private CardValue (int value)
      {
        this.cardValue = value;
      }

      public int getCardValue() {
        return cardValue;
      }
    }

套装类

package finalProjectGolf;

public enum Suit
{
  HEARTS(1),
  SPADES(2),
  CLUBS(3),
  DIAMONDS(4);

  private int suit;

  private Suit (int value)
  {
    this.suit = value;
  }

  public int getCardSuit() {
    return suit;
  }
}

2 个答案:

答案 0 :(得分:2)

要添加到AaronB的答案,您的printDeck方法实际上是错误的,因为您已发布它。目前它打印空字符串52次。此外,您只需要打印套牌中的所有项目,就不需要三次循环。一个非常简单的实现,可以在一个单独的牌组的新线上打印每张牌:

public void printDeck() {
    for(Card card : deck) {
        System.out.println( card.toString() );
    }
}

您还需要为枚举值覆盖toString方法,以便它们打印您想要的名称。请查看https://stackoverflow.com/a/14413605/1425014了解如何执行此操作。

您的主要课程名为DeckOfCards。这向我表明,这个班级代表了一副牌。但是,根据for (int d = 0; d < numDecks; d++)private static int numDecks = 1判断,您似乎打算让DeckOfCards代表一张或多张卡牌。如果您需要的不仅仅是DeckOfCards而不是使DeckOfCards类复杂化,那么简单地使用集合(例如ArrayList)可能更清楚。

最后,在此处发布代码之前,您应该尝试确保您的注释有意义。从DeckOfCards构造函数复制/粘贴后,printDeck()函数的注释没有更改。

答案 1 :(得分:1)

问题不在于你的printDeck方法不起作用(我还没有测试过,但乍一看它看起来很合理),这是你永远不会打电话的。您可以将它放在DeckOfCards构造函数的末尾,如果要检查它是否正确。

此外,你真的应该重构一堆你的逻辑,尤其是在DeckOfCards类中。你有一些很大的计算块 - 把它放在一个方法中。此外,您应该在构造函数中声明它们,而不是在类中声明变量。例如:

ArrayList<Card> deck; //array of Card objects

private int currentCard; // index of next Card to be dealt (0-51)
private static int numDecks;

//constructor fill deck of Cards
public DeckOfCards(int numDecks){
    deck = new ArrayList<Card>();
    currentCard = 0; //set currentCard so first Card dealt is deck[0]
    this.numDecks = numDecks;

如果我错了,请纠正我,你没有真正描述问题是什么......