我在程序的最后一点上遇到了一些麻烦。 我不太确定如何在我的数组中计算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来显示总值。
任何帮助将不胜感激!谢谢:))
答案 0 :(得分:1)
您可以这样做,删除类卡上的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函数,因为你正在创建一个类的实例,如果你计算不同的类实例就没有意义。所以宁可把它作为静态函数,然后你可以从那里进行计算。
答案 1 :(得分:1)
我认为Sum
不应成为Card
的一部分。这意味着任何单个Card
实例都知道你的整个手牌,但事实并非如此。你当前的程序中缺少“手”对象的概念,我认为引入一个新类会增加一些必要的职责分离。这个新类Hand
应该包含您的Card
数组以及您的处理逻辑:
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
答案 2 :(得分:1)
对您的代码进行了一些更改,我将对其进行解释。
类卡
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}");
}