I'm learning control flow and trying to write code for grab 5 different numbers, store then into an array, sort and print. Im getting exception saying the array is out of bounds, but i can't see why, since im storing 5 objects into a [4] array.
Any tips why im getting this exception?
public static void Three()
{
var numbers = new int[4];
var i = 0;
while (i <= 4)
{
Console.WriteLine("enter a number: ");
var input = Convert.ToInt32(Console.ReadLine());
if (Array.IndexOf(numbers, input) != -1)
{
Console.WriteLine("try again");
}
else
{
numbers[i] = input;
i++;
}
}
Array.Sort(numbers);
foreach (var item in numbers)
{
Console.WriteLine(Convert.ToString(item));
}
}