0

I am trying to figure out a way to search and check an array while filling it up. I want to use the overloaded equals method I have created from another class.

Here is the overloaded equals method in the public class Order : IComparable

public override bool Equals(object obj)
    {

            Order temp = (Order)obj;
        if (orderNumber == temp.orderNumber)
            return true;
        else 
            return false;

    }

in main, I try to create the 2 arrays so I can compare 1 to the other and check for dups as I go through with a nested for loop but it is not working. It just continues filling the array as if it never saw the second for loop

public static void Main()

    {
    //Create an array of five ShippedOrder objects. 
        ShippedOrder[] myShippedOrder = new ShippedOrder[5];
        ShippedOrder[] checkDup = new ShippedOrder[5];

    //Prompt the user for values for each Orders object; do NOT allow duplicate order numbers and force the user to reenter 
    //    the order when a duplicate order number is entered.



            for (int x = 0; x < myShippedOrder.Length; ++x)
            {

                int y = 0;
                myShippedOrder[x] = new ShippedOrder();
                checkDup[y] = new ShippedOrder();

                Console.Write("Please enter the order number for order {0}:  ", x + 1);
                checkDup[y].orderNumber = Convert.ToInt32(Console.ReadLine());             


                for (y = 0; y < checkDup.Length; ++y)
                {
                    if (checkDup[y] != null && myShippedOrder[x].Equals(checkDup[y]))
                    {
                        Console.WriteLine("Sorry, this order number has already been entered, please try again");
                        --x;
                        break;
                    }
                    else myShippedOrder[x].orderNumber = checkDup[y].orderNumber;
                }

                Console.Write("Please enter the customers name for order {0}:  ", x + 1);
                myShippedOrder[x].customerName = Console.ReadLine();

                Console.Write("Please enter the quantity that was ordered for order {0}:  ", x + 1);
                myShippedOrder[x].quantityOrdered = Convert.ToInt32(Console.ReadLine());

                myShippedOrder[x].totalPrice = myShippedOrder[x].quantityOrdered * PRICEEACH + ShippedOrder.SHIPFEE;


        }

any suggestions on how to use the overloaded equal method in the order class to help in finding duplicate orderNumbers if they exist and kick back to the user they entered an invalid order and to try again with that spot in the array?

4

1 回答 1

1

这里发生的事情是continue当用户输入错误时您需要这样做。

尝试这个:

public static void Main()

{
//Create an array of five ShippedOrder objects. 
    ShippedOrder[] myShippedOrder = new ShippedOrder[5];
    ShippedOrder[] checkDup = new ShippedOrder[5];

//Prompt the user for values for each Orders object; do NOT allow duplicate order numbers and force the user to reenter 
//    the order when a duplicate order number is entered.


        bool wronginput = false;
        for (int x = 0; x < myShippedOrder.Length; ++x)
        {

            int y = 0;
            myShippedOrder[x] = new ShippedOrder();
            checkDup[y] = new ShippedOrder();

            Console.Write("Please enter the order number for order {0}:  ", x + 1);
            checkDup[y].orderNumber = Convert.ToInt32(Console.ReadLine());             


            for (y = 0; y < checkDup.Length; ++y)
            {
                if (checkDup[y] != null && myShippedOrder[x].Equals(checkDup[y]))
                {
                    Console.WriteLine("Sorry, this order number has already been entered, please try again");
                    --x;
                    wronginput = true;
                    break;
                }
                else myShippedOrder[x].orderNumber = checkDup[y].orderNumber;
            }

            if(wronginput)
                continue;

            Console.Write("Please enter the customers name for order {0}:  ", x + 1);
            myShippedOrder[x].customerName = Console.ReadLine();

            Console.Write("Please enter the quantity that was ordered for order {0}:  ", x + 1);
            myShippedOrder[x].quantityOrdered = Convert.ToInt32(Console.ReadLine());

            myShippedOrder[x].totalPrice = myShippedOrder[x].quantityOrdered * PRICEEACH + ShippedOrder.SHIPFEE;


    }

请注意我是如何添加wronginput的,以便在第二个 for 循环之后立即返回到外部循环的开头。

我认为这就是你现在正在寻找的东西,即使有更好的方法来做你正在做的事情来检查重复。

于 2012-04-19T02:21:08.753 回答