0

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

        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());
        }           
4

2 回答 2

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。我在记事本中写了这个,所以它可能不起作用,但应该给你一个关于如何解决你的问题的一般指南。

于 2012-06-18T00:07:40.417 回答
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++;
}
于 2012-06-18T00:08:44.540 回答