简体   繁体   English

传递数组有麻烦

[英]having trouble with passing array

I'm having issues with passing array values. 我在传递数组值时遇到问题。 I've been working on this for hours and can't seem to find the answer. 我已经为此工作了好几个小时,似乎找不到答案。 wonder if someone can point me in the right direction. 想知道是否有人可以指出我正确的方向。 This is what I have so far. 到目前为止,这就是我所拥有的。 I've looked at countless examples, videos and reading material and can't seem to come up with the solution. 我看过无数的示例,视频和阅读材料,似乎无法提出解决方案。 I would really appreciate the help. 我非常感谢您的帮助。

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;
        }
}

} }

I need it to print like this. 我需要像这样打印。

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 . . .

But I'm getting this: 但是我得到这个:

    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 . . .

There are two problems - one explaining why you're only using the first input number, and one explaining why you're only comparing it against one random number. 有两个问题-一个解释了为什么只使用第一个输入数字,另一个解释了为什么只将它与一个随机数进行比较。

For the random number part, here's the problem: 对于随机数部分,这是问题所在:

Seed = (int)DateTime.Now.TimeOfDay.Ticks;
random[x] = getRnd(Seed);   
...
static int getRnd(int Seed)
{
    Random myrandomnum = new Random(Seed);

Unless DateTime.Now.TimeOfDay.Ticks changes between iterations - which is very unlikely, as the system clock resolution is usually in the order of milliseconds, and you're not doing a lot of work between iterations - you're creating multiple instances of Random , all with the same seed. 除非DateTime.Now.TimeOfDay.Ticks在两次迭代之间发生变化-这是非常不可能的,因为系统时钟分辨率通常为毫秒级,并且两次迭代之间您不会做很多工作-您将创建多个Random ,所有种子相同。 That means you'll get the same sequence of random numbers from each instance - but you're actually only asking for one number from each instance anyway. 这意味着您将从每个实例中获得相同的随机数序列-但实际上您无论如何都只从每个实例中请求一个数。

The solution is to create a single Random instance, and then use that for every random number you generate. 该解决方案是创建一个 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;
}

Of course at that point you may consider removing the method entirely, and using: 当然,此时您可以考虑完全删除该方法,并使用:

Random rng = new Random();
for (int x = 0; x < random.Length; x++)
{
    random[x] = random.Next(1, 100);
}  

You might also want to consider using random.Next(1, 100) if you want a value between 1 and 100 inclusive - the second argument to Random.Next is an exclusive upper bound. 您可能还需要考虑使用random.Next(1, 100)如果你想1和100之间值-的第二个参数Random.Next是一个独特的上限。

Note that this approach to creating a single instance of Random doesn't play well in a multi-threaded scenario, as Random isn't thread-safe. 注意,这种创建单个Random实例的方法在多线程方案中效果不佳,因为Random不是线程安全的。 Fortunately that's not a problem here, but see my article on Random for more information if you need it later. 幸运的是,这不是问题,但是如果您以后需要的话,请参阅我有关Random文章,以获取更多信息。

For the "only using a single input value" problem, LaD1Da's answer goes into more detail, but the issue is that inputNumber is always input[0] ... you're never using input[1] , input[2] etc. 对于“仅使用单个输入值”问题,LaD1Da的答案会更详细,但问题是inputNumber始终为input[0] ...您从未使用过input[1]input[2]等。

I have not enough 'Reputation' to simply add a comment: 我没有足够的“信誉”来简单地添加评论:

The issue is that you do not change the inputnumber in your last loop. 问题是您没有在上一个循环中更改输入号。 You always use input[0] . 您始终使用input[0] Simply change the last loop to the following: 只需将最后一个循环更改为以下内容:

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]);
    }
}

Edit: Additionally you have an issue with your random number generation. 编辑:此外,您的随机数生成存在问题。 Refer to Jon Skeet's answer for that. 有关此信息,请参阅Jon Skeet的答案。

There are two problems. 有两个问题。

The first problem is in getRnd() . 第一个问题是在getRnd() On each iteration of the loop you are passing a new seed, but the loop is executing so fast that it is likely that the number of Ticks will be the same on each iteration. 在循环的每次迭代中,您都传递了一个新的种子,但是循环执行得如此之快,以至于Ticks的数量在每次迭代中都可能是相同的。 You can fix this by moving the seeding outside of the loop, and passing the Random generator as a parameter to the getRnd() function: 您可以通过以下方式解决此问题:将种子移出循环,然后将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);                
}  

The second problem is in your loop. 第二个问题在您的循环中。 On this line: 在这行上:

int inputnumber=input[0];

You assign the value in input[0] to inputnumber . 您可以将input[0]的值分配给inputnumber However, you never change it on future iterations of the loop. 但是,您永远不会在循环的将来迭代中更改它。 You can fix this by iterating through input[] at each loop iteration, ie: 您可以通过在每次循环迭代中遍历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]);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM