gpt4 book ai didi

c# - System.Array不包含添加的定义

转载 作者:行者123 更新时间:2023-12-02 10:48:25 24 4
gpt4 key购买 nike

我正在制作二十一点游戏,到目前为止,我已经完成了纸牌类,套牌类和鞋类。纸牌类(class)适用于甲板类(class),鞋类适用,但我仍在手工类(class)中工作。我创建了一个方法,如果手中已经有MAX_CARDS张卡片,则会抛出异常;否则,它将添加该卡片到手中并递增_cardCount,但是出于某种原因,我的代码_hand.Add(card)

System.Array does not contain a definition for Add.



任何在正确方向上的帮助或指导将不胜感激

这是我的手工课的内容
class Hand
{
const Int32 MAX_CARDS = 12;

private Card[] _hand = new Card[MAX_CARDS];
private Int32 _cardCount = 0;

public Int32 CardCount
{
get
{
return _cardCount;
}
}

public void AddCard(Card card)
{
if (_cardCount >= MAX_CARDS)
{
throw new Exception("Cannot of more than 12 cards in a hand");
}
else
{
_hand.Add(card);
_cardCount++;
}
}

public Card GetCard(Int32 cardIndex)
{
if (cardIndex >= _cardCount)
{
throw new Exception("Invalid Entry");
}
else
{
return _hand[cardIndex];
}
}

Int32[] cardValues = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10 };
String[] cardSymbols = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" };

private Int32 SymbolToValue(String symbol)
{
int index = Array.IndexOf(cardSymbols, symbol);
if (index != -1)
{
return cardValues[index];
}
else
{
throw new Exception("Value Not In Table");
}
}

public Int32 GetSum()
{
int value = 0;
Boolean aceflag;
for (int i = 0; i < _hand.Length; i++)
{
value += SymbolToValue(_hand[i].Value);
if (String.Equals(_hand[i].Value, "A"))
{
aceflag = true;
}
else
{
aceflag = false;
}
if (aceflag == true && value <= 21)
{
value = value + 10;
}
}
return value;
}
}

最佳答案

我认为您的代码有很多问题,但是如果您确实想按照自己的方式进行操作,请使用索引添加一个项目:

public void AddCard(Card card)
{
if (_cardCount >= MAX_CARDS)
{
throw new Exception(string.Format("This hand already contains {0} cards, " +
"which is the maximum.", MAX_CARDS));
}
else
{
_hand[_cardCound] = card;
_cardCount++;
}
}

关于c# - System.Array不包含添加的定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27351984/

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