【问题标题】:c# calculating integer within arrayc# 计算数组内的整数
【发布时间】:2017-05-04 01:43:34
【问题描述】:

我的程序的最后一点有点麻烦。 我不太确定如何在我的数组中计算 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 显示总值。

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

【问题讨论】:

    标签: c# arrays visual-studio sum int


    【解决方案1】:

    您可以改为这样做,删除类 Card 上的 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();
        }
    

    我在你的类之外删除了这个 Sum 函数,因为你正在创建一个类的实例,如果你正在计算不同的类实例,这将没有意义。所以宁愿把它作为静态函数,然后你可以从那里计算。

    【讨论】:

      【解决方案2】:

      对您的代码进行了一些更改,我会解释这些。

      班级卡片

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

      注意变化:

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

      带有静态方法 Sum 的卡片类

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

      对 ProcessHand 方法的更改

          static int ProcessHand(Card[] cards)
          {
              var sum = 0;
              foreach (var card in cards)
              {
                  card.DisplayCard();
                  sum += card.FaceValue;
              }
      
              return sum;
          }
      
      • 遍历卡片列表和DisplayCard()并添加到sum
      • 不使用Sum(),因为它再次遍历数组(在foreach 循环之后),这似乎没有必要。
      • 该方法返回卡片的总和(我个人的架构决定)

      主要

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

      【讨论】:

        【解决方案3】:

        我认为Sum 不应该是Card 的一部分。这意味着任何一个Card 实例都知道你的整只手,但它不知道。您当前的程序中缺少“手”对象的概念,我认为引入一个新类会增加一些必要的职责分离。这个新类Hand 应该包含您的Cards 数组和您的处理逻辑:

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

        您可以向Hand 添加一个名为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
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-01-31
          • 2021-03-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-12-20
          • 2016-10-21
          • 1970-01-01
          相关资源
          最近更新 更多