我正在玩生成随机数,我可以让它们生成,虽然我的console.writeline(randomList [i]);似乎循环两次然后显示两个结果,它应该只显示每个循环一个值,并且用户必须按任意键才能获得下一个结果。有人能对此有所了解吗?感谢。
class Program
{
static void Main(string[] args)
{
GenerateRandomNumbers();
}
private static void GenerateRandomNumbers()
{
//Initialize an array
int[] randomList = new int[1000];
//Initialize an instance of random class
Random rnd = new Random();
// integer variable
int counter = 0;
while (counter < 1000)
{
//store random num
int random = rnd.Next(1, 1001);
if (Array.IndexOf(randomList, random) <= 0)
{
//store random number into Array
randomList[counter] = random;
counter++;
}
}
//output elements in Array
for (int i = 0; i < 1000; i++)
{
Console.WriteLine(randomList[i]);
Console.Read();
}
//output number of elements in Array
// Console.WriteLine(counter);
Console.Read();
}
答案 0 :(得分:1)
解决方案是在循环中使用Console.ReadLine()而不是Console.Read()