我正试图编写一个程序,将13张扑克牌从王牌转换为王牌。处理2张卡片并合计分配给每张卡片的值。 ace = 11,king = 10,jack = 10,queen = 10,ten = 10,nine = 9,eight = 8等等......有点像二十一点。C#给数组赋值
到目前为止,我只能随机洗牌并随机打印两张牌,但我不知道如何为每张牌分配一个值,并将其添加并打印出来。例如,如果我的两个随机卡是国王和八的话,我想程序打印出来..
王
八
这里是我得到的...
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'类。或者,创建一个名称和值之间映射的“字典”。 –
Rob
如上所述,您应该使用“集合”,如“KeyValuePair”或“Dictionary”。 – Greg
在循环中调用'Random random = new Random();'会导致很多(如果不是全部的话)随机数是相同的。你应该只使用一个“Random”实例。 – Enigmativity