【问题标题】:Java Printing Out a Deck of Cards with an ArrayList and Nested For LoopsJava 用 ArrayList 和嵌套的 For 循环打印出一副卡片
【发布时间】:2018-07-05 07:51:39
【问题描述】:

我需要使用 ArrayList 和嵌套的 for 循环打印出一副包含 52 张独特卡片的卡片。我只使用数组就完成了这项工作,但我无法使用 ArrayList 代替。任何帮助表示赞赏,谢谢。

实例变量:

    public final String[] SUITS = {"Hearts", "Diamonds", "Spades", "Clubs"};
    public final String[] DESCRIPTIONS = {"Ace", "Two", "Three", "Four", 
                                          "Five", "Six", "Seven", "Eight", 
                                          "Nine", "Ten", "Jack", "Queen", 
                                          "King"};

    private ArrayList<Card> deck;

将我的牌组添加到 ArrayList 的方法:

public void loadDeck1()
{
    deck = new ArrayList<Card>();

    for(int i = 0; i < DESCRIPTIONS.length; i++)
    {
        for(int j =0; j < SUITS.length; j++)
        {
            **deck.add(new Card(DESCRIPTIONS[i], SUITS[j])); 
            // BlueJ Error: actual and formal argument lists differ in length**
        }
    }
}

打印甲板的方法:

public void printDeck()
{
    for(Card c : deck)
    {
        System.out.println(c.getSuit() + " of " + c.getDescription());
    }
}

编辑:对不起,这是我的卡片类!

public class Card
{
private String suit;
private String description;

/**
 * Constructor for objects of class Card
 */
public Card()
{
    suit = null;
    description = null;
}

/**
 * Accessors
 */

/**
 * @return the suit of the card
 */
public String getSuit() 
{
    return this.suit;
}

   /**
    * @return the description of the card
    */
public String getDescription()
{
    return this.description;
}

/**
 * Mutators
 */

/**
 * @param the suit of the card
 */
public void setSuit(String suit)
{
    this.suit = suit;
}

/**
 * @param the description of the card
 */
public void setDescription(String description)
{
  this.description = description;
}    

}

【问题讨论】:

  • 你需要一个 Card 中的构造函数,它需要两个 Strings。
  • @Stymieceptive 你没有列出你的 Card 类实现,你有吗?
  • 你的构造函数是什么样子的?似乎参数的数量不匹配,您需要@notyou 提到的 2 个参数。
  • 对不起,是的,我有一个!刚刚编辑了它。

标签: java arraylist printing nested-loops playing-cards


【解决方案1】:

你需要添加一个构造函数,它接受两个字符串(西装,描述)并为卡片类创建一个卡片对象,因为你将在deck.add(new Card(DESCRIPTIONS[i], SUITS[j]));语句中使用构造函数。如果你不添加这个构造函数,你可以更改描述或通过 setMethods 设置卡片对象的西装变量。

public Card(String s , String d)
{
    suit = s;
    description = d;
}

如果您想使用 ArrayList 而不是数组作为实例变量。

public ArrayList<String> SUITS = add("Hearts");
ArrayList<String> SUITS = add("Diamonds");
ArrayList<String> SUITS = add("Spades");
ArrayList<String> SUITS = add("Clubs");

public ArrayList<String> DESCRIPTIONS = add("Ace");/...
//and add elements of descriptions arrayList

您还可以将 ArrayList 传递给普通的 for 循环而不是 for each 循环。

需要修改部分代码:

for(int i = 0; i < DESCRIPTIONS.size(); i++)
{
    for(int j =0; j < SUITS.size(); j++)
    {
        **deck.add(new Card(DESCRIPTIONS.get(i), SUITS.get(j))); 
    }
} 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-14
    • 1970-01-01
    • 1970-01-01
    • 2019-02-27
    • 2018-04-04
    • 1970-01-01
    • 1970-01-01
    • 2012-04-06
    相关资源
    最近更新 更多