Я занимаюсь домашней проблемой, когда мне нужно написать приложение, которое вводит пять чисел, каждый из которых составляет от 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