我试图通过使用一些循环的方法添加一个代表食品的对象。 这是方法:
public boolean addItem(FoodItem other)
{
boolean isAdded = false;
int i = 0;
while(i<_stock.length)
{
if(_stock[i]!=null)
{
if (_stock[i].equals(other))
{
_stock[i].setQuantity(_stock[i].getQuantity()+other.getQuantity());
System.out.println(_stock[i]+"equals");
isAdded = true;
i++;
break;
}
else if (_stock[i].getName().equals(other.getName())
&& _stock[i].getCatalogueNumber()==other.getCatalogueNumber()
&& _stock[i].getExpiryDate().equals(other.getExpiryDate()) != true)
{
for(int x=i ; x<_stock.length; x++)
{
if (_stock[x] == null)
{
_stock[x] = new FoodItem(_stock[x-1]);
System.out.println(_stock[i]+"x-1");
break;
}
}
_stock[i] = new FoodItem(other);
System.out.println(_stock[i]+"expiery dates");
isAdded = true;
break;
}
else if (_stock[i].getName().equals(other.getName())
&& _stock[i].getCatalogueNumber()==other.getCatalogueNumber()
&& _stock[i].getProductionDate().equals(other.getProductionDate()) != true)
{
for(int x=i ; x<_stock.length; x++)
{
if (_stock[x] == null)
{
_stock[x] = new FoodItem(_stock[x-1]);
System.out.println(_stock[i]+"x-1");
break;
}
}
_stock[i] = new FoodItem(other);
System.out.println(_stock[i]+"production dates");
isAdded = true;
i++;
break;
}
else
{
System.out.println(_stock[i]);
System.out.println(_stock[i]+"i++");
i++;
break;
}
}
else
{
_stock[i] = new FoodItem(other);
System.out.println(_stock[i]+"else");
isAdded = true;
i++;
break;
}
}
return isAdded;
}
但是当我尝试添加对象时,我得到一个奇怪的输出,在这里我调用该方法:
Date t1 = new Date(1,1,2000);
Date t2 = new Date(1,1,2001);
Date t3 = new Date(1,1,2002);
FoodItem f1 = new FoodItem("Milk", 1111, 12, t1, t2, 7, 10, 5);
FoodItem f2 = new FoodItem("Milk", 1111, 12, t1, t2, 7, 10, 5);
FoodItem f3 = new FoodItem("PopCorn", 3333, 2, t1, t3, 6, 10, 12);
Stock s = new Stock();
s.addItem(f1);
s.addItem(f2);
s.addItem(f3);
System.out.println(s._stock[0]+"added");
System.out.println();
System.out.println(s._stock[1]+"added");
System.out.println();
System.out.println(s._stock[2]+"added");
这是输出:
食品编号:Milk_catalogue编号:1111_生产日期:01/01/2000 _到期日期:01/01/2001 _数量:12else FoodItem:Milk _catalogueNumber:1111_productionDate:01/01/2000 _expiryDate:01/01/2001 _quantity:24equals FoodItem:Milk _catalogueNumber:1111_productionDate:01/01/2000 _expiryDate:01/01/2001 _quantity:24 FoodItem:Milk _catalogueNumber:1111_productionDate:01/01/2000 _expiryDate:01/01/2001 _quantity:24i ++ FoodItem:Milk _catalogueNumber:1111_productionDate:01/01/2000 _expiryDate:01/01/2001 _quantity:24added
未添加
未添加
当我将添加的项目更改为仅其他项目时
FoodItem f1 = new FoodItem("Milk", 1111, 12, t1, t2, 7, 10, 5);
FoodItem f2 = new FoodItem("Cola", 1111, 12, t1, t2, 7, 10, 5);
FoodItem f3 = new FoodItem("PopCorn", 3333, 2, t1, t3, 6, 10, 12);
输出为:
食品编号:Milk_catalogue编号:1111_生产日期:01/01/2000 _到期日期:01/01/2001 _数量:12else FoodItem:Milk _catalogueNumber:1111_productionDate:01/01/2000 _expiryDate:01/01/2001 _quantity:12 FoodItem:Milk _catalogueNumber:1111_productionDate:01/01/2000 _expiryDate:01/01/2001 _quantity:12i ++ FoodItem:Milk _catalogueNumber:1111_productionDate:01/01/2000 _expiryDate:01/01/2001 _quantity:12 FoodItem:Milk _catalogueNumber:1111_productionDate:01/01/2000 _expiryDate:01/01/2001 _quantity:12i ++ FoodItem:Milk _catalogueNumber:1111_productionDate:01/01/2000 _expiryDate:01/01/2001 _quantity:12added
未添加
未添加
我在做什么错?似乎从来没有真正尝试添加除拳头之外的任何其他项目...
答案 0 :(得分:1)
使用此代码来使代码更具可读性/效率肯定有很多问题,但是我认为您的直接问题是由于使用break
语句引起的。 Break
将完全退出循环,我相信您正在寻找的是continue
语句。这将退出当前迭代,然后再次运行循环(假设条件仍然成立)。