我正在尝试找出一种在填充数据时搜索和检查数组的方法。我想使用我从另一个类创建的重载equals方法。
以下是公共类Order:IComparable
中重载的equals方法public override bool Equals(object obj)
{
Order temp = (Order)obj;
if (orderNumber == temp.orderNumber)
return true;
else
return false;
}
在main中,我尝试创建2个数组,这样我可以比较1和另一个并检查重复,因为我使用嵌套for循环,但它无法正常工作。它只是继续填充数组,好像它从未看过第二个for循环
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;
}
关于如何在订单类中使用重载的equal方法来帮助查找重复的orderNumbers(如果它们存在并且回到用户输入无效订单并再次尝试使用数组中的那个点)的任何建议?
答案 0 :(得分: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循环之后,它会返回到外循环的开头。
我认为这就是你现在正在寻找的东西,即使有更好的方法来做你正在做的事情来检查重复。