Java 如何编写初始化52个卡对象的构造函数

Java 如何编写初始化52个卡对象的构造函数,java,Java,这就是我需要做的: 此构造函数使用52个卡对象初始化卡组,表示标准卡组中的52张卡。这些牌必须从黑桃王牌到钻石王牌 以下是我的尝试: private Card[] cards; String suit, card; private final int DECK_SIZE = 52; public Deck() { cards = new Card[DECK_SIZE]; String suit[] = {"spades", "hearts", "clovers", "diam

这就是我需要做的:

此构造函数使用52个卡对象初始化卡组,表示标准卡组中的52张卡。这些牌必须从黑桃王牌到钻石王牌

以下是我的尝试:

private Card[] cards;
String suit, card;
private final int DECK_SIZE = 52;

public Deck() 
{
    cards = new Card[DECK_SIZE];
    String suit[] = {"spades", "hearts", "clovers", "diamonds"};
    String card[] = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Joker", "Queen", "King"};
    for (int c = 0; c<13; c++)
        for (int s = 0; s<4; s++)
        {
            cards.equals(new Card(suit, card));

        }



}
private Card[]卡;
线装、卡片;
私人最终内部甲板尺寸=52;
公共甲板()
{
卡片=新卡片[卡片组大小];
弦乐套装[]={“黑桃”、“红心”、“三叶草”、“钻石”};
字符串卡[]={“A”、“2”、“3”、“4”、“5”、“6”、“7”、“8”、“9”、“10”、“小丑”、“女王”、“国王”};

对于(int c=0;c我不确定equals方法的作用,但您正在传递完整的数组。请尝试此

cards.equals(new Card(suit[s], card[c]));
我不确定是否允许我们添加额外的构造函数。不过,为我们编写的代码中确实包含一张卡片(int,int)

只有你的导师或助教才能回答这个问题。我们不是无所不知的

至于您当前的版本:

您确定要制作一张带有套装和卡类型列表的
卡吗

即使构造函数是存在的,我也不认为
.equals
做了你认为它做的事。我会查阅关于这个问题的文档。

可能是这样的:

cards = new Card[DECK_SIZE];
String suits[] = {"spades", "hearts", "clovers", "diamonds"};
String cards[] = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Joker", "Queen", "King"};

int cardIndex = 0;
for (String suit : suits) {
    for (String card : cards) {
        cards[cardIndex] = new Card(suit, card);
        cardIndex++;
    }
}

将卡片视为数字(1-13)并对显示的Ace、Jack、Queen和King进行文本替换可能是一种更好的方法。“
构造函数卡片(String[],String[])未定义。
”说明了一切。你给了它套装和卡片。它们被定义为
字符串套装[]
字符串卡片[]
。它们都是字符串数组。这真的是你想要制作卡片的材料吗?不,更好的方法是与给你布置作业的讲师交谈,以便更好地了解如何使用他们的卡片类。另请参见此。可能重复+1,指出
的错误用法。等于
cards = new Card[DECK_SIZE];
String suits[] = {"spades", "hearts", "clovers", "diamonds"};
String cards[] = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Joker", "Queen", "King"};

int cardIndex = 0;
for (String suit : suits) {
    for (String card : cards) {
        cards[cardIndex] = new Card(suit, card);
        cardIndex++;
    }
}