我正在尝试打印分配给特定数组的数字数组。我选择数字的算法包括选择一个不重复的随机数并将其存储在数组中。
真的很简单,但我不知道为什么它会打印出这个错误。
int[] ticket1 = new int[4];
for (int i = 0; i < 4; i++)
{
int temp = rand.Next(43);
while (ticket1.Contains(temp))
{
temp = rand.Next(43);
}
ticket1[i] = temp;
}
Console.WriteLine("{0}{1}", item.PadRight(20), ticket1.ToString());//ticket1 produces System.Int32[] instead of 4 numbers.
//I have changed this line to:
//Console.WriteLine("{0}{1}", item.PadRight(20), string.Join(",", ticket1));
//And it still doesn't work. the error remains. (System.Int32[])
我的问题是,如何以字符串格式打印出我的 4 个数字(彼此并排)。
//编辑:我发现了我的问题。我将ticket1 放在foreach 循环中,它以某种方式无法访问数组值,因此它打印出 System.Int32[] 。
都修好了。