我所拥有的一切都运行良好,但我正在寻找一种方法来提示用户是否输入了重复的订单号并让他们重新输入新的订单号。现在它只接受重复,并不显示我想要的错误消息。我创建了两个类Order
基础和ShippedOrder
。这是我在Order
类中的equals方法。
public override bool Equals(Object e)
{
bool equal;
Order temp = (Order)e;
if (orderNumber == temp.orderNumber)
equal = true;
else
equal = false;
return equal;
这是我在main方法中的内容。如果有帮助,我可以张贴所有内容。我正在寻找帮助,找到一种方法来阻止用户表单输入相同的两个订单号。
static void Main(string[] args)
{
double sum = 0;
ShippedOrder[] orderArray = new ShippedOrder[5];
ShippedOrder[] check = new ShippedOrder[5];
bool wrong = true;
for (int x = 0; x < orderArray.Length; ++x)
{
orderArray[x] = new ShippedOrder();
Console.Write("Enter order number: ");
orderArray[x].orderNumber = Convert.ToInt32(Console.ReadLine());
for (int y = 0; y < x; ++y)
{
check[y] = new ShippedOrder();
if (orderArray[x].Equals(check[y]))
wrong = false;
while (!wrong)
{
Console.WriteLine("Sorry, the order number {0} is a duplicate. \nPlease reenter {1}",
orderArray[x], check[y]);
for (y = 0; y < x; ++y)
{
if (orderArray[x].Equals(check[y]))
wrong = false;
}
check[y] = orderArray[x];
}
}
Console.Write("Enter cusomer name: ");
orderArray[x].customerName = Console.ReadLine();
Console.Write("Enter quanity: ");
orderArray[x].quanityOrdered = Convert.ToInt32(Console.ReadLine());
答案 0 :(得分:0)
我会用LINQ做这个,这是它真正发光的地方(如果这段代码没有被称为每秒千次)
var arrayExpectedCount = 5;
var orderArray = new List<ShippedOrder>(); //define array of orders to generate
var check = new List<ShippedOrder>(); //array of orders already shipped,already present
......
//infinit interation
while(true) {
var orderNumber = Convert.ToInt32(Console.ReadLine());
if(!check.Exist(o=>o.orderNumber == orderNumber)
{
//create a new order and push it to the orderArray
}
//break condition of the infinit loop
if(orderArray.Count == arrayExpectedCount)
break;
}
如果这不是您所要求的,请澄清。
答案 1 :(得分:0)
List<ShippedOrder> orderArray = new List<ShippedOrder>();
do {
int newOrderNum = 0;
// Retrieve first attempt
Console.Write("Enter order number: ");
newOrderNUm = Conver.ToInt32(ConsoleReadLine());
// Check if any of the orders in orderArray have the same order number
while(orderArray.Any(a=>a.orderNumber == newOrderNum) {
Console.Write("Sorry, the order number is a duplicate. Please enter re-enter the order number: ");
newOrderNum = Conver.ToInt32(ConsoleReadLine());
};
orderArray.Add(new ShippedOrder() {
orderNumber = newOrderNum,
};
} while(orderArray.Length < 5);