Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# c计算数组中的整数_C#_Arrays_Visual Studio_Sum_Int - Fatal编程技术网

C# c计算数组中的整数

C# c计算数组中的整数,c#,arrays,visual-studio,sum,int,C#,Arrays,Visual Studio,Sum,Int,我的程序的最后一部分有点问题。 我不太确定如何计算数组中的int。 以下是我目前的代码: class Tester { static void Main(string[] args) { Card[] hand = { new Card("Spade", 3), new Card("Club", 10), ne

我的程序的最后一部分有点问题。 我不太确定如何计算数组中的int。 以下是我目前的代码:

class Tester
{
    static void Main(string[] args)
    {
        Card[] hand = {
                          new Card("Spade", 3),
                          new Card("Club", 10),
                          new Card("Diamond", 11),
                          new Card("Heart", 9),
                          new Card("Diamond", 13),
                      };
        ProcessHand(hand);
    }//end of static void main

    static void ProcessHand(Card[] cards)
    {
        cards[0].DisplayCard();
        cards[1].DisplayCard();
        cards[2].DisplayCard();
        cards[3].DisplayCard();
        cards[4].DisplayCard();
    }//end of static void processhand
}//end of class Tester


class Card
{
    public string suit { get; set; }
    public int facevalue { get; set; }
    public Card (string su, int fa) 
    {
        suit = su;
        facevalue = fa;
    }
    public int Sum(params int[] facevalue)// <- trying to calculate sum of facevalues of cards.
    {
        return facevalue.Sum();
    }
    public void DisplayCard()
    {
        Console.WriteLine("The card is  {0,-10} {1,-10}", suit, facevalue);

    }
}//end of class Card
我有点困惑,首先把代码放在哪里,因为我正试图把它放在课堂卡片上。我已经尝试了我所知道的所有东西的不同变体,在这两个类中,但似乎都不起作用。我只是想对顶部显示的所有卡片的面值求和,然后使用console.writeline显示总值


任何帮助都将不胜感激!谢谢:

您可以这样做,删除您的课程卡上的Sum函数,并将其作为静态函数放在静态void Main下:

private static void Main(string[] args)
    {
        Card[] hand =
            {
                new Card("Spade", 3),
                new Card("Club", 10),
                new Card("Diamond", 11),
                new Card("Heart", 9),
                new Card("Diamond", 13),
            };
        ProcessHand(hand);
        Console.WriteLine(Sum(hand.Select(x=>x.facevalue).ToArray()));
    }

    static int Sum(params int[] facevalue)// <- trying to calculate sum of facevalues of cards.
    {
        return facevalue.Sum();
    }
我在您的类之外删除了这个求和函数,因为您正在创建一个类的实例,如果您正在计算类的不同实例,那么这个实例就没有意义了。所以,不如把它作为静态函数,这样你们就可以从这里开始计算了。

我不认为求和应该是卡的一部分。这意味着任何一张牌实例都知道你的整张牌,但事实并非如此。当前程序中缺少手对象的概念,我认为引入一个新类会增加一些必要的职责分离。这个新的手牌应该包含你的牌阵列和你的处理逻辑:

public class Hand
{
    private readonly Card[] _cards;

    public Hand(Card[] cards)
    {
        _cards = cards;
    }

    public void Process()
    {
        // You can use a loop and avoid hard-coding the indices
        foreach (var card in _cards)
        {
            card.DisplayCard();
        }
    }
}
另外,您可以添加一个名为TotalFaceValue的方法或只读属性来计算总面值

// Read-only property
public int TotalFaceValue
{
    get { return _cards.Sum(c => c.facevalue); }
}

// Method
public int GetTotalFaceValue()
{
    return _cards.Sum(c => c.facevalue);
}
我会选择前者。总之,您的新代码可能如下所示:

class Tester
{
    static void Main(string[] args)
    {
        // Fill a new hand with your cards
        var hand = new Hand(new [] {
            new Card("Spade", 3),
            new Card("Club", 10),
            new Card("Diamond", 11),
            new Card("Heart", 9),
            new Card("Diamond", 13),
        });

        // "process" (display) your cards values
        hand.Process();

        // Shows the total hand value. This could also be moved into
        // the Process() function if you want
        Console.WriteLine("Total face value: {0}", hand.TotalFaceValue);

    } //end of static void main


}//end of class Tester

public class Hand
{
    private readonly Card[] _cards;

    public Hand(Card[] cards)
    {
        _cards = cards;
    }

    public void Process()
    {
        foreach (var card in _cards)
        {
            card.DisplayCard();
        }
    }

    public int TotalFaceValue
    {
        get { return _cards.Sum(c => c.facevalue); }
    }
}    

class Card
{
    public string suit { get; set; }
    public int facevalue { get; set; }
    public Card (string su, int fa) 
    {
        suit = su;
        facevalue = fa;
    }

    public void DisplayCard()
    {
        Console.WriteLine("The card is  {0,-10} {1,-10}", suit, facevalue);

    }
}//end of class Card

对您的代码做了一些更改,我将对此进行解释

班级卡

注意这些变化:

属性的命名约定 属性现在是只读的 Sum方法不属于Card类。该方法与卡对象没有直接关系。它与Card类有关系。因此,在类中可以有一个静态方法,如: 使用静态方法Sum的Card类

ProcessHand方法的更改

迭代卡片列表和DisplayCard并将其添加到总和 不使用Sum,因为它在foreach循环之后再次迭代数组,这似乎是不必要的。 该方法返回我个人架构决策的卡片总数 主要

    class Card
    {
        public string Suit { get; }
        public int FaceValue { get; }
        public Card(string su, int fa)
        {
            Suit = su;
            FaceValue = fa;
        }

        public void DisplayCard()
        {
            Console.WriteLine("The card is  {0,-10} {1,-10}", Suit, FaceValue);
        }
    }
    class Card
    {
        public string Suit { get; }
        public int FaceValue { get; }
        public Card(string su, int fa)
        {
            Suit = su;
            FaceValue = fa;
        }

        public void DisplayCard()
        {
            Console.WriteLine("The card is  {0,-10} {1,-10}", Suit, FaceValue);
        }

        public static int Sum(Card[] cards)
        {
            return cards.Sum(c => c.FaceValue);
        }
    }
    static int ProcessHand(Card[] cards)
    {
        var sum = 0;
        foreach (var card in cards)
        {
            card.DisplayCard();
            sum += card.FaceValue;
        }

        return sum;
    }
    public static void Main(string[] args)
    {
        Card[] hand = {
                      new Card("Spade", 3),
                      new Card("Club", 10),
                      new Card("Diamond", 11),
                      new Card("Heart", 9),
                      new Card("Diamond", 13),
                  };
        var sum = ProcessHand(hand);
        Console.WriteLine($"The sum is {sum}");
    }