【发布时间】:2015-10-10 08:24:55
【问题描述】:
我正在尝试编写一个程序,将 13 张扑克牌从王牌洗牌。发 2 张牌并将分配给每张牌的值相加。 ace = 11,king = 10,jack = 10,queen = 10,10 = 10,9 = 9,8 = 8 等等......有点像二十一点。
到目前为止,我只能洗牌并随机打印出两张牌,但我不知道如何为每张牌分配一个值,将它们相加并打印出来。例如,如果我的两张随机牌是国王和八,那么我希望程序打印出来..
国王
八
18
这就是我得到的......
static void Main(string[] args)
{
string[] Cards = new string[] {"Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "jack", "Queen", "King", "Ace"};
for (int x = 0; x < 100; x++) // looping the shuffle 100 times to maximize the randomness
{
for (int i = Cards.Length; i > 0; i--) //shuffle
{
string temp;
Random random = new Random();
int r = random.Next(i);
temp = Cards[r];
Cards[r] = Cards[i-1];
Cards[i-1] = temp;
}
}
Console.WriteLine(Cards[0]); //first random card
Console.WriteLine(Cards[1]); //second random card
Console.ReadKey();
}
【问题讨论】:
-
创建一个存储名称和值的
Card类。或者,创建一个在名称和值之间映射的Dictionary<string, int>。 -
如上所述,您应该使用
Collection,例如KeyValuePair或Dictionary。 -
在循环中调用
Random random = new Random();将导致许多(如果不是全部)随机数相同。您应该只使用Random的单个实例。