无法弄清楚如何为此代码添加边界

时间:2015-10-24 17:42:56

标签: c# arrays conditional-statements

我在尝试插入用户输入必须遵守的边界时遇到问题。该程序应该让用户在 0-100 之间输入7个整数。 0-100边界应该阻止用户输入负整数,同时将数字限制为99 。用户输入的整数存储在一个数组中,如果他/她输入一个重复的输入,该条件将删除插入的非法输入并再次询问用户。这很难,因为逻辑上不知道如何在我已经编写的代码中实现这样的条件。

namespace ConsoleApplication1
{
    class ArrayInput
    {
        static void Main(string[] args)
        {
            int input; //user input variable
            int[] array; //declared array
            array = new int[7]; //array has a given one dimensional quantity
            Console.WriteLine("This program will ask you to input 7 numbers only, no duplications.");


                for (input = 0; input <= 6; input++)
                {
                    try
                    {
                        Console.WriteLine("\nPlease enter your numbers.");
                        array[input] = Convert.ToInt32(Console.ReadLine()); //input can only take in integers
                    }

                    catch (FormatException)
                    {
                        Console.WriteLine("That input will not work, it is set to 0 and removed. Please try again.");
                        --input;
                    }

                    while (array.Take(input).Contains(array[input])) 
                    {
                        Console.WriteLine("This is a duplicated input, please try again.");
                        --input; //this removes the input that was wrong from the array
                    }

                     Console.WriteLine("Your last entered integer: " + array[input]); //print out the element so that user can verify

                }

                for (input = 0; input <= 6; input++)
                {
                    Console.WriteLine("\nYou entered these: " + array[input]); //prints out all elements inputted from algorithm above
                }

            Console.ReadLine();
        }
    }
}

我将0-100边界条件放在哪里?这也是一个模型,所以这不是最终的代码,因此请不要对此负面。我只需要想法。

0 个答案:

没有答案