gpt4 book ai didi

java - 类中的 "store"对象是什么意思?

转载 作者:行者123 更新时间:2023-11-30 06:50:42 26 4
gpt4 key购买 nike

我正在做一个 Java 家庭作业,上面写着:

The programming projects of Chapter 4 discussed a Card class that represents a standard playing card. Create a class called DeckofCards that stores 52 objects of the Card class. Include methods to shuffle the deck, deal a card, and report the number of cards left in the deck. The shuffle method should assume you have a full deck. Create a driver class with a main method that deals each card from a shuffled deck, printing each card as it is dealt.

它说的地方

Create a class called DeckofCards that stores 52 objects of the Card class.

我不确定存储某些东西意味着什么。这是否意味着将 52 个对象创建到一个类中?或者,假设 Card 类中已经创建了 52 个对象,将所有这些对象放在另一个对象或构造函数中?我确实做了一个卡片类,但是,我没有制作 52 个对象,卡片组中也有 52 张卡片,所以我假设 52 个对象与 52 张卡片相同。无论如何,如果需要,这里是我的卡片类的代码:

import java.util.Random;

public class CardClass {

private static Random generator = new Random();
private int suit, num;
private String cardSuit, cardNum;

public CardClass() {
shuffle();
}

public void shuffle() {
num = generator.nextInt(13) + 1;
suit = generator.nextInt(4) + 1;
}

public String toString() {
if (num >=2 && num <= 10)
cardNum = Integer.toString(num);
else if (num == 1)
cardNum = "Ace";
else if (num == 11)
cardNum = "Jack";
else if (num == 12)
cardNum = "Queen";
else
cardNum = "King";

if (suit == 1)
cardSuit = "Spades";
else if (suit == 2)
cardSuit = "Hearts";
else if (suit == 3)
cardSuit = "Clubs";
else
cardSuit = "Diamonds";

return cardNum + " of " + cardSuit;
}
}

这也是我第一次来到这个社区,所以对于我犯的任何错误,我深表歉意,并在此先感谢您的所有帮助。

最佳答案

这意味着您的 DeckOfCards 类应该包含一个大小为 52 的数组,用于存储 Card 实例。你总是可以使用另一种数据结构,比如链表。该数组可能应该是 DeckOfCards 中的私有(private)属性,例如

private Card cards[]=new Card[52];

然后用 Card 对象填充数组:

for(int i=0;i<52;i++){
int cardType=i/13;
String type="Heart";
if(cardType==0)type="Spades";
else if(cardType==1)type="Clubs";
else if(cardType==2)type="Diamond";
cards[i]=new Card(i, cardType);
}

此外,您最好在 Stack Overflow 中提问编程问题。

关于java - 类中的 "store"对象是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41115674/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com