【问题标题】:System.Array does not contain definition for AddSystem.Array 不包含 Add 的定义
【发布时间】:2015-02-05 18:05:26
【问题描述】:

我正在创建一个二十一点游戏,到目前为止,我已经制作了纸牌类、套牌类和鞋类。卡片类有效,套牌类有效,鞋类有效,但我仍在学习手牌类。我创建了一个方法,如果手中已经有 MAX_CARDS 卡,则会引发异常,否则它将卡添加到手上并增加 _cardCount 但出于某种原因,我的代码 _hand.Add(card)

System.Array 不包含 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;
    }
}

【问题讨论】:

  • 出于某种原因... system.Array 不包含添加的定义。 ...我不确定如何解决这个问题。 It doesn't have an Add method - at best it's got IList.Add which is not supported。使用允许您添加内容的集合。
  • 数组的大小是固定的,请改用List&lt;Card&gt;
  • 这是我看到的与二十一点游戏相同代码的第三个问题。我只是想知道在我们最终实现你的游戏之前会有多少问题

标签: c# arrays compiler-errors


【解决方案1】:

C# 中 Array 的大小是不可变的,因此我建议改用 List。

private List<Card> _hand = new List<Card>(MAX_CARDS);

【讨论】:

  • 数组当然不是不可变的。
  • 数组的大小为immutable(实际上是固定的)。数组本质上是可变的。
【解决方案2】:

我认为你的代码有很多问题,但是如果你真的想按照你的方式去做,使用索引来添加一个项目:

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++;
    }
}

【讨论】:

    【解决方案3】:

    在此处输入代码,我们创建一个新列表,其中包含已存在的数组中的元素。我们使用 List 构造函数并将数组传递给它。 List 接收此参数并从中填充其值。

    注意:数组元素类型必须与List元素类型匹配,否则编译会失败。

    将数组复制到列表的程序:C#

    using System;
    using System.Collections.Generic;
    
    class Program
    {
    
        static void Main()
        {
    
            int[] arr = new int[3]; // New array with 3 elements
    
            arr[0] = 2;
    
    
            arr[1] = 3;
    
            arr[2] = 5;
    
            List<int> list = new List<int>(arr); // Copy to List
    
            Console.WriteLine(list.Count);       // 3 elements in List
    
         }
    
    }
    
    
    Output : 3
    

    Array 不适合您在 c# 中的代码 list is used array 不是一个好的选择。所以使用列表类型数据。您可以使用列表并将每种类型的数据存储在此对象中并将其转换为任何类型,您还可以使用循环从列表中获取单个数据。它很容易使用和实现,所以使用 list 它对代码来说既好又容易。

    首先,我们声明一个 int 值列表。我们创建一个未指定大小的新列表,并向其中添加四个素数。值按添加的顺序存储。还有其他创建列表的方法——这不是最简单的。

    using System.Collections.Generic;
    
    class Program
    {
    
      static void Main()
    
       {
    
         List<int> list = new List<int>();
    
         list.Add(2);
    
         list.Add(3);
    
         list.Add(5);
    
         list.Add(7);
    
       }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2013-11-15
      • 2014-11-23
      • 2012-09-29
      • 2015-07-24
      • 1970-01-01
      • 2011-06-21
      • 2021-08-16
      • 2014-12-19
      • 1970-01-01
      相关资源
      最近更新 更多