如何强制重复输入?

时间:2012-06-17 23:59:48

标签: c# duplicates user-input

此代码适用于我,除了它只检查一次重复,如果用户再次输入相同的整数,程序将第二次接受它。无论用户输入多少次,如何使该程序不接受该条目?

        int[] shippedOrderNumbers = new int[5];

        Console.WriteLine("Please Input first Shipping Number: ");
        shippedOrderNumbers[0] = Convert.ToInt32(Console.ReadLine());           

        Console.WriteLine("Please Input second Shipping Number: ");
        shippedOrderNumbers[1] = Convert.ToInt32(Console.ReadLine());           


        if (shippedOrderNumbers[0] == shippedOrderNumbers[1])
        {
            Console.WriteLine("A dublicate number is not allowed please try again: ");
            shippedOrderNumbers[1] =Convert.ToInt32(Console.ReadLine());               
        }          

            Console.WriteLine("Please Input third Shipping Number: ");
            shippedOrderNumbers[2] = Convert.ToInt32(Console.ReadLine());


        if (shippedOrderNumbers[0] == shippedOrderNumbers[2] || shippedOrderNumbers[1] == shippedOrderNumbers[2])
        {
            Console.WriteLine("A dublicate number is not allowed please try again: ");
            shippedOrderNumbers[2] = Convert.ToInt32(Console.ReadLine());
        }                           
            Console.WriteLine("Please Input fourth Shipping Number: ");
            shippedOrderNumbers[3] = onvert.ToInt32(Console.ReadLine());               


        if (shippedOrderNumbers[0] == shippedOrderNumbers[3] || shippedOrderNumbers[1] == shippedOrderNumbers[3] || shippedOrderNumbers[2] == shippedOrderNumbers[3])
        {
            Console.WriteLine("A dublicate number is not allowed please try again: ");
            shippedOrderNumbers[3] = Convert.ToInt32(Console.ReadLine());
        }                           
            Console.WriteLine("Please Input fifth Shipping Number: ");
            shippedOrderNumbers[4] = Convert.ToInt32(Console.ReadLine());


        if (shippedOrderNumbers[0] == shippedOrderNumbers[4] || shippedOrderNumbers[1] == shippedOrderNumbers[4] || shippedOrderNumbers[2] == shippedOrderNumbers[4] || shippedOrderNumbers[3] == shippedOrderNumbers[4])
        {
            Console.WriteLine("A dublicate number is not allowed please try again: ");
            shippedOrderNumbers[4] = Convert.ToInt32(Console.ReadLine());
        }           

2 个答案:

答案 0 :(得分:3)

我认为使用通用列表而不是数组会更好。您还可以使用简单的循环来简化代码。请参阅以下示例。

IList<int> shippedOrderNumbers = new List<int>();

for (int = 0; i < 5; i++)
{
    Console.WriteLine("Please input the Shipping Number: ");

    int shippingNumber;
    if (!Int.TryParse(Console.ReadLine(), out shippingNumber) 
    {
        Console.WriteLine("Your shipping number is not an integer");
    }

    if (shippedOrderNumbers.Contains(shippingNumber)) 
    {
        Console.WriteLine("This shipping number has already been entered.");
    }
    else 
    {
        shippedOrderNumbers.Add(shippingNumber);
        Console.WriteLine("Thanks for submitting your shipping number.");
    }
}

PS。我在记事本中写了这个,所以可能不起作用,但应该给你一个如何解决问题的一般指南。

答案 1 :(得分:0)

这些方面应该做的事情:

var shippedOrderNumbers = new List<int>();

var validOrderNumbers = 0;

while(validOrderNumbers < 5)
{
     Console.WriteLine("Input order number:");
     var nextOrderNumber = Convert.ToInt32(Console.ReadLine());
     if(shippedOrderNumbers.Any(son => son == nextOrderNumber))
     {
         Console.WriteLine("Duplicate order number. Please enter another");
         continue;
     }
     shippedOrderNumbers.Add(nextOrderNumber);
     validOrderNumbers++;
}