【发布时间】:2020-04-19 17:26:14
【问题描述】:
随机生成扑克牌的类:
class Card
{
private string face;
private string suit;
//Constructor
public Card()
{
string[] faceArray = {"Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven", "Eight"
, "Nine", "Ten", "Jack", "Queen", "King"};
string[] suitArray = { "Clubs", "Hearts", "Spades", "Diamonds" };
Random rand = new Random();
face = faceArray[rand.Next(0, 13)];
Thread.Sleep(150);
Random dand = new Random();
suit = suitArray[dand.Next(0, 4)];
}
//Method face of suit
public override string ToString()
{
return face + " of " + suit;
}
}
我认为这可以正常工作,但问题出在:
class DeckOfCards
{
private Card[] deck = new Card[52];
//constructor fills deck with unique cards
public DeckOfCards()
{
for (int i = 0; i < 52; i++)
{
Card tempCard = new Card();
if (deck.Contains(tempCard))
i--;
else
deck[i] = tempCard;
}
}
}
即使我有:
if (deck.Contains(tempCard))
i--;
套牌仍然产生重复。我是不是用错了数组方法?
【问题讨论】:
-
你创建了一张新的临时卡,然后检查卡组是否包含它,为什么会返回 true?
-
临时卡有面值和花色值。我的目的是检查数组是否包含具有相同属性的卡。我不确定我是否正确使用它
-
那么你应该检查属性的值,而不是对象。不要使用 contains 方法。
-
我不知道该怎么做。我将如何比较对象的值?
-
对不起,我不太清楚你所说的“包含功能”是什么意思。
.Contains 已经在 Visual Studios 的 c# 中