我正在做一个作业问题,我需要编写一个输入五个数字的应用程序,每个数字都在10到100之间(包括10和100)。当每个数字被读取时,只有当它不是已经读取的数字的副本时才显示它。提供最糟糕的情况,其中所有五个数字都不相同。为了解决这个问题,尽可能使用最小的数组,并在用户输入每个新值后显示一组完整的唯一值。使用数组显示一组唯一的值
我到目前为止工作正常。只有当程序运行时,如果所有五个数字都是唯一的,我会一直收到未处理的错误。它告诉我索引超出了数组的范围。只有我不确定它为什么这样做,而我在for循环中看不到任何错误。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ten_Seven_Thirteen
{
class Program
{
static void Main(string[] args)
{
const int SIZE = 5;
int[] nums = new int[SIZE];
int a;
int b;
int c = 0; //c is the amount of numbers currently entered
int number;
Console.WriteLine("Input five numbers between 10 and 100 (inclusive): \n");
for (int count = 0; count < SIZE; count++)
{
number = Convert.ToInt32(Console.ReadLine());
if (number >= 10 && number <= 100) //Verify whether or not the number meets the criteria
{
for (a = 0; a < c; a++)
{
// The following if condition checks for duplicate entrees.
if (number == nums[a])
{
Console.WriteLine("Duplicate number.\n");
break; //Breaking the loop prevents the duplicate number from entering the array
}
} // end for
// if number is not a duplicate, enter it in array
if (number != nums[a])
{
nums[c++] = number;
} // end if - not duplicate
Console.WriteLine("The non-duplicate values are:\n");
//display array of unique numbers
for (b = 0; nums[b] != 0 && b < SIZE; b++)
{
Console.WriteLine(nums[b]);
} // end for loop and display array
} // end if - validate and test
else
Console.WriteLine("invalid number.");
Console.WriteLine("\n");
} // end for - get 5 numbers
}
}
}// end Ten_Seven_Thirteen