【问题标题】:Can't seem to print an arraylist full of objects/enums似乎无法打印一个充满对象/枚举的数组列表
【发布时间】:2013-12-10 05:30:02
【问题描述】:

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

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

DeckOfCards 类

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


    }

卡值类

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;
  }
}

【问题讨论】:

  • 问题是……顺便说一句,你为什么不在你的所有类/枚举中覆盖toString 方法?

标签: java arrays printing arraylist enums


【解决方案1】:

为了补充 AaronB 的答案,您的 printDeck 方法实际上是错误的,正如您发布的那样。目前它打印空字符串 52 次。此外,您不需要为了打印卡组中的所有项目而重复三次 for 循环。一个非常简单的实现,将每张卡片打印在一个新行上,用于单副牌:

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 &lt; numDecks; d++)private static int numDecks = 1 来看,您似乎打算让 DeckOfCards 代表一副或多副纸牌。如果您需要的不仅仅是在 DeckOfCards 上而不是使 DeckOfCards 类复杂化,那么简单地使用集合(例如 ArrayList)可能会更清楚。

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

【讨论】:

  • 伟大的 cmets,也感谢您检查打印方法。 @anonymous:许多新程序员往往过于拘泥于特定的语法;退后一步,从逻辑上慢慢地思考它是编写任何高质量代码的一个非常重要的部分。 “我真的需要这样做吗?这有意义吗?”在计划和编写代码时,您应该始终在脑海中思考。
【解决方案2】:

问题不在于你的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;

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

【讨论】:

  • 有几个问题,您的建议确实有很大帮助,谢谢!
猜你喜欢
  • 2018-05-18
  • 2021-09-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-09
相关资源
最近更新 更多