我遇到传递数组值的问题。我已经工作了几个小时,似乎无法找到答案。想知道是否有人可以指出我正确的方向。这就是我到目前为止所拥有的。我看过无数的例子,视频和阅读材料,似乎无法提出解决方案。我非常感谢你的帮助。
static void Main(string[] args)
{
int Seed = 0;
int[] random = new int[10];
int[] input = new int[10];
for (int x = 0; x < random.Length; x++)
{
Seed = (int)DateTime.Now.TimeOfDay.Ticks;
random[x] = getRnd(Seed);
}
for (int x = 0; x < input.Length; x++)
{
Console.Write("Enter an integer number between 1 and 100: ");
input[x] = Convert.ToInt32(Console.ReadLine());
}
int inputnumber=input[0];
for (int x = 0; x < input.Length; x++)
if (inputnumber <= random[x])
{
Console.WriteLine("The entered number " + inputnumber + " is less than " + random[x]);
}
else if (inputnumber >= random[x])
{
Console.WriteLine("The entered number " + inputnumber + " is greater than " + random[x]);
}
else if (inputnumber == random[x])
{
Console.WriteLine("The entered number " + inputnumber + " is equal to " + random[x]);
}
}
static int getRnd(int Seed)
{
Random myrandomnum = new Random(Seed);
int randomvalue = myrandomnum.Next(1, 100);
return randomvalue;
}
}
}
我需要它像这样打印。
Enter an integer number between 1 and 100: 1
Enter an integer number between 1 and 100: 8
Enter an integer number between 1 and 100: 44
Enter an integer number between 1 and 100: 22
Enter an integer number between 1 and 100: 16
Enter an integer number between 1 and 100: 88
Enter an integer number between 1 and 100: 41
Enter an integer number between 1 and 100: 77
Enter an integer number between 1 and 100: 10
Enter an integer number between 1 and 100: 52
The entered number 1 is less than 64
The entered number 8 is less than 44
The entered number 44 is less than 80
The entered number 22 is less than 91
The entered number 16 is less than 95
The entered number 88 is greater than 39
The entered number 41 is less than 79
The entered number 77 is greater than 27
The entered number 10 is less than 35
The entered number 52 is less than 65
Press any key to continue . . .
但我得到了这个:
Enter an integer number between 1 and 100: 1
Enter an integer number between 1 and 100: 8
Enter an integer number between 1 and 100: 44
Enter an integer number between 1 and 100: 22
Enter an integer number between 1 and 100: 16
Enter an integer number between 1 and 100: 88
Enter an integer number between 1 and 100: 41
Enter an integer number between 1 and 100: 77
Enter an integer number between 1 and 100: 10
Enter an integer number between 1 and 100: 52
The entered number 1 is less than 64
The entered number 1 is less than 64
The entered number 1 is less than 64
The entered number 1 is less than 64
The entered number 1 is less than 64
The entered number 1 is less than 64
The entered number 1 is less than 64
The entered number 1 is less than 64
The entered number 1 is less than 64
The entered number 1 is less than 64
Press any key to continue . . .
答案 0 :(得分:2)
有两个问题 - 一个解释了为什么你只使用第一个输入数字,另一个解释了为什么你只将它与一个随机数进行比较。
对于随机数部分,问题在于:
Seed = (int)DateTime.Now.TimeOfDay.Ticks;
random[x] = getRnd(Seed);
...
static int getRnd(int Seed)
{
Random myrandomnum = new Random(Seed);
除非DateTime.Now.TimeOfDay.Ticks
在迭代之间发生变化 - 这是非常不可能的,因为系统时钟分辨率通常在毫秒级,并且你在迭代之间没有做很多工作 - 你&#39;重新创建Random
的多个实例,所有实例都使用相同的种子。这意味着您将从每个实例中获得相同的随机数序列 - 但实际上您实际上只是要求每个实例中的一个数字。
解决方案是创建一个单个 Random
实例,然后将其用于您生成的每个随机数。
Random rng = new Random();
for (int x = 0; x < random.Length; x++)
{
random[x] = GenerateRandomValue(rng);
}
...
static int GenerateRandomValue(Random random);
{
int randomvalue = random.Next(1, 100);
return randomvalue;
}
当然,此时您可以考虑完全删除该方法,并使用:
Random rng = new Random();
for (int x = 0; x < random.Length; x++)
{
random[x] = random.Next(1, 100);
}
如果您想要一个介于1和100 之间的值,您可能还需要考虑使用random.Next(1, 100)
- Random.Next
的第二个参数是 exclusive 上限。
请注意,这种创建Random
单个实例的方法在多线程方案中不能很好地发挥作用,因为Random
不是线程安全的。幸运的是,这不是问题,但如果您以后需要,请参阅my article on Random
以获取更多信息。
对于&#34;仅使用单个输入值&#34;问题,LaD1Da的答案更详细,但问题是inputNumber
总是input[0]
...你永远不会使用input[1]
,{{1}等等。
答案 1 :(得分:1)
我没有足够的“声誉”来简单地添加评论:
问题是您不会更改上一个循环中的输入数字。您始终使用input[0]
。只需将最后一个循环更改为以下内容:
for (int x = 0; x < input.Length; x++)
int inputnumber = input[x]; // This is new
if (inputnumber <= random[x])
{
Console.WriteLine("The entered number " + inputnumber + " is less than " + random[x]);
}
else if (inputnumber >= random[x])
{
Console.WriteLine("The entered number " + inputnumber + " is greater than " + random[x]);
}
else if (inputnumber == random[x])
{
Console.WriteLine("The entered number " + inputnumber + " is equal to " + random[x]);
}
}
修改:此外,您的随机数生成存在问题。请参阅Jon Skeet的答案。
答案 2 :(得分:0)
有两个问题。
第一个问题出在getRnd()
。在循环的每次迭代中,您传递一个新种子,但循环执行得如此之快,以至于每次迭代时Ticks的数量可能相同。您可以通过将循环移动到循环外部并将Random
生成器作为参数传递给getRnd()
函数来解决此问题:
Seed = (int)DateTime.Now.TimeOfDay.Ticks;
Random rand = new Random(Seed);
for (int x = 0; x < random.Length; x++)
{
random[x] = getRnd(rand);
}
第二个问题出在你的循环中。在这一行:
int inputnumber=input[0];
您将input[0]
中的值指定给inputnumber
。但是,您永远不会在循环的未来迭代中更改它。你可以通过在每次循环迭代中迭代input[]
来解决这个问题,即:
for (int x = 0; x < input.Length; x++)
{
if (input[x] <= random[x])
{
Console.WriteLine("The entered number " + input[x] + " is less than " + random[x]);
}
else if (input[x] >= random[x])
{
Console.WriteLine("The entered number " + input[x] + " is greater than " + random[x]);
}
else if (input[x] == random[x])
{
Console.WriteLine("The entered number " + input[x] + " is equal to " + random[x]);
}
}