So I have an Array with 100 int elements, that have to be drawn randomly within the range [-100,100], and has to return the amount of numbers that are odd and are within this range [-30,30]. And I do this:
int counter = 0;
int i = 0;
int[] numbers = new int[100];
for ( i = 0; i <= numbers.Length; i++)
{
Random rnd = new Random();
numbers[i] = rnd.Next(-100, 101);
if (numbers[i] % 2 != 0)
{
if (numbers[i] >= -30 && numbers[i] <= 30)
{
counter++;
}
}
}
Console.WriteLine(counter);
And I build and receive no errors. But when running I receive this error on the command prompt: System.IndexOutOfRangeException: Index was outside the bounds of the array. Then it guides me to this line:
numbers[i]=rnd.Next(-100,101);
So, like, what's happening? What is the wrong thing I did?
Aucun commentaire:
Enregistrer un commentaire