简体   繁体   English

System.Array不包含添加的定义

[英]System.Array does not contain definition for Add

I am creating a blackjack game and so far I have made a card class, deck class and shoe class. 我正在制作二十一点游戏,到目前为止,我已经完成了纸牌类,甲板类和鞋类。 The card class works the deck class works, the shoe class works but I am still working on my hand class. 纸牌课程适用于甲板课程,鞋类适用,但我仍在手工课程中工作。 I created a method that throws an exception if there are already MAX_CARDS cards in the hand otherwise it adds the card to the hand and increments _cardCount but for some reason on my code _hand.Add(card) says that 我创建了一个方法,如果手中已经有MAX_CARDS张卡片,则会抛出异常,否则它将添加卡片到手中并增加_cardCount,但是由于我的代码_hand.Add(card)原因,

System.Array does not contain a definition for Add. System.Array不包含添加的定义。

Any help or guidance in the right direction would be appreciated 任何在正确方向上的帮助或指导将不胜感激

Here's what I have for my hand class 这是我的手工课的内容

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

c#中数组的大小是不可变的,因此我建议改用List。

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

I think your code has a lot of problems, but if you really want to do it the way you're doing it, use the index to add an item: 我认为您的代码有很多问题,但是如果您确实想按照自己的方式进行操作,请使用索引添加一个项目:

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

enter code here we create a new List with the elements in an array that already exists. 在这里输入代码,我们使用数组中已经存在的元素创建一个新的List。 We use the List constructor and pass it the array. 我们使用List构造函数并将其传递给数组。 List receives this parameter and fills its values from it. 列表接收此参数并从中填充其值。

Caution:The array element type must match the List element type or compilation will fail. 警告:数组元素类型必须与List元素类型匹配,否则编译将失败。

Program that copies array to List: C# 将数组复制到列表的程序: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 is not good for your code in c# list is used array is not a good option. 数组对您在c#列表中的代码不好,使用数组不是一个好的选择。 So use list type data. 因此,请使用列表类型数据。 You can use list and store every type data in this object and convert it in any type and you can also get single data from list using a loop. 您可以使用列表并将每个类型的数据存储在此对象中,并将其转换为任何类型,还可以使用循环从列表中获取单个数据。 It so easy to use it and implement so use list it good and easy for code . 它是如此易于使用和实现,因此使用list对代码来说又好又容易。

First, we declare a List of int values. 首先,我们声明一个int值列表。 We create a new List of unspecified size and adds four prime numbers to it. 我们创建了一个未指定大小的新列表,并为其添加了四个素数。 Values are stored in the order added. 值按添加顺序存储。 There are other ways to create Lists—this is not the simplest. 还有其他创建列表的方法-这不是最简单的方法。

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);

   }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.Array&#39; 不包含 &#39;Count&#39; 的定义 - System.Array' does not contain a definition for 'Count' System.Array不包含“拆分”的定义 - System.Array does not contain a definition for 'Split' System.Array不包含ToArray的定义 - System.Array does not contain a definition for ToArray System.Array 不包含“Split”的定义并且没有扩展方法“Split” - System.Array does not contain a definition for 'Split' And no extension method "Split" 我收到一个错误“ System.Array不包含LastWriteTime的定义” - i get an error “System.Array does not contain a definition for LastWriteTime” system.array不包含ToArray的定义,也没有扩展方法 - system.array does not contain a definition for ToArray and no extension method 名称冲突? &#39;System.Array&#39;不包含&#39;item&#39;的定义 - Name Conflict? 'System.Array' does not contain a definition for 'item' 错误95&#39;System.Array&#39;不包含&#39;FindIndex&#39;的定义 - Error 95 'System.Array' does not contain a definition for 'FindIndex' System.Array不包含“任何”的定义-C# - System.Array does not contain a definition for 'Any' - C# 为什么我用这个WinRT代码得到&#39;&#39;System.Array&#39;不包含&#39;AsBuffer&#39;的定义? - Why am I getting “'System.Array' does not contain a definition for 'AsBuffer'” with this WinRT code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM