IndexOutOfBounds错误

时间:2018-03-01 12:45:35

标签: c#

所以我是编程新手。我已经打印了两个彼此相邻的阵列。在左栏中,我有Ace,2,3,4 ..直到杰克,女王。一个正确的栏目是我的套房。所以俱乐部,心脏等现在我想随机化我的整个套牌,但我发现只有例如3个心印,没有别的。另外,我收到IndexOutOfBounds错误。我无法弄清楚原因。

这是我的代码:

string[] suites = new string[] { "hearts", "clubs", "diamonds", "spades" };

string [] pack = new string[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13" };

Random r = new Random();
string[] x = new string[] { suites[r.Next(0,3)].ToString() };
string[] y = new string[] { pack[r.Next(0,12)].ToString() };

for (int a = 0; a < 13; a++)
{
    for (int p = 0; p < 4; p++)
    {
        Console.WriteLine(y[a] + " " + x[p]);
    }
}

1 个答案:

答案 0 :(得分:1)

替换以下行

string[] x = new string[] { suites[r.Next(0,3)].ToString() };
string[] y = new string[] { pack[r.Next(0,12)].ToString() };

string[] x = suites.OrderBy(a => r.Next()).ToArray();
string[] y = pack.OrderBy(a => r.Next()).ToArray();