[英]c# calculating integer within array
我在程序的最后一点上遇到了一些麻烦。 我不太确定如何在我的数组中计算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();
}
我在你的类之外删除了这个Sum函数,因为你正在创建一个类的实例,如果你计算不同的类实例就没有意义。 所以把它作为静态函数,然后你可以从那里进行计算。
我不认为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
对您的代码进行了一些更改,我会解释这些。
类卡
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}");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.