在填充数组时需要帮助检查数组中的重复条目
本文关键字:数组 帮助 填充 检查 | 更新日期: 2023-09-27 18:31:40
我正在尝试找出一种方法来搜索和检查数组,同时填充它。 我想使用我从另一个类创建的重载等于方法。
这是公共类顺序中的重载等于方法:
public override bool Equals(object obj)
{
Order temp = (Order)obj;
if (orderNumber == temp.orderNumber)
return true;
else
return false;
}
总的来说,我尝试创建 2 个数组,以便我可以将 1 与另一个进行比较,并在使用嵌套 for 循环时检查 dup,但它不起作用。 它只是继续填充数组,就好像它从未看到第二个 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;
}
关于如何在 order 类中使用重载的 equal 方法来帮助查找重复的 orderNumber(如果存在)并回馈给他们输入无效订单的用户并再次尝试数组中的该位置的任何建议?
这里发生的事情是,当用户输入错误的内容时,您需要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 循环之后,它立即回滚到外部循环的开头。
我认为这就是您现在正在寻找的,即使有更好的方法来执行您正在做的事情来检查重复项。