简体   繁体   中英

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

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. Fortunately that's not a problem here, but see my article on Random for more information if you need it later.

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.

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

There are two problems.

The first problem is in 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. You can fix this by moving the seeding outside of the loop, and passing the Random generator as a parameter to the getRnd() function:

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 . However, you never change it on future iterations of the loop. You can fix this by iterating through input[] at each loop iteration, ie:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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