错误索引超出了数组的范围。程序是从数组中删除重复项我理解错误是第 32 行,在 for 循环的末尾创建了一个额外的元素,但我无法找到一种方法来显示没有重复项的整个数组。prog 要做的是将一个 5 数字输入到一个数组中,然后对它们进行排序,如果它们是重复的,则将其删除。我希望我说清楚了!
using System;
class duplicate
{
static void Main()
{
const int Array_Size = 5;
int [] number = new int [Array_Size];
int i;
for ( i = 0; i < Array_Size; i++)
{
number[i] = Int32.Parse(Console.ReadLine());
if (number[i] < 9 || number[i] > 101)
{
Console.WriteLine("Enter Number between 10 - 100");
number[i] = Int32.Parse(Console.ReadLine());
}
}
Array.Sort(number);
Console.WriteLine("Sorted Array : ");
for (i = 0; i < Array_Size; i++)
{
Console.WriteLine("Element is " + number[i]);
}
Console.WriteLine("Duplicate Removed : ");
for (i = 0; i < Array_Size; i++)
{
if (number[i] != number[i+1])
Console.WriteLine("Element is " + number[i]);
}
Console.ReadLine();
}
}