所以我做了一些作业,其中我有一个类 FoodItem
和类 Stock
。在“Stock”类中,有一个 FoodItem
(_stock
) 数组和数组中当前位置的整数 (_noOfFoodItems
)。
其中一项任务是查找数组中最昂贵的商品(FoodItem
的类属性之一是 int _price
)。另一条指令是,如果数组为空,则该方法应返回 null。我已经写入了每个属性的 get
和 set
方法。
起初,在我看来,这就像任何其他“找到数组中最大的项目”问题一样,但由于某种原因,我似乎很难解决它。这是我到目前为止得到的结果:
public FoodItem mostExpensive() {
if (_noOfFoodItems == 0) {
return null;
}
FoodItem mostExpensiveFoodItem = _stock[0];
for (int i = 0 ; i< _noOfFoodItems; i++) {
if (_stock[i].getPrice() > mostExpensiveFoodItem.getPrice()) {
mostExpensiveFoodItem = new FoodItem(_stock[i]);
}
}
return mostExpensiveFoodItem;
}
我真的不知道出了什么问题。大学告诉我们使用的 IDE 没有调试功能,而且代码对我来说似乎很好。
我已经对其进行了测试,该方法返回第一个项目而不是最昂贵的项目。
你能告诉我我做错了什么吗? 如果您认为错误与代码的其他部分有关,请告诉我要添加哪一部分。
提前谢谢您!
编辑
我在该方法中使用的导体如下:
public FoodItem(FoodItem otherFoodItem)
{
this._name = otherFoodItem.getName();
this._catalogueNumber = otherFoodItem.getCatalogueNumber();
this._quantity = otherFoodItem.getQuantity();
this._prodactionDate = otherFoodItem.getProdactionDate();
this._expiryDate = otherFoodItem.getExpiryDate();
this._minTemperature = otherFoodItem.getMinTemperature();
this._maxTemperature = otherFoodItem.getMaxTemperature();
}
我的测试主要:
public static void Main (String[] Args)
{
Stock s = new Stock();
s.addItem(new FoodItem ("milk", 1111, 5, new Date (30, 5, 2003), new Date (31, 5, 2003), -16, 22, 17 ));
s.addItem(new FoodItem ("milk", 1111, 3, new Date (30, 5, 2003), new Date (31, 5, 2003), -16, 22, 17 ));
s.addItem(new FoodItem ("bread", 1112, 2, new Date (7, 6, 2001), new Date (13, 6, 2002), -7, 1, 13 ));
s.addItem(new FoodItem ("corn", 1113, 1, new Date (30, 5, 2001), new Date (30, 5, 2000), -16, 22, 18 ));
s.addItem(new FoodItem ("soup", 1111, 5, new Date (30, 5, 2003), new Date (31, 5, 2003), -16, 22, 17 ));
s.addItem(new FoodItem ("hot dog", 1114, 201, new Date (30, 5, 2007), new Date (31, 5, 2003), 7, 5, 1 ));
System.out.println(s.mostExpensive().getName());
}
“常规”构造器:
public FoodItem (String name, long catalogueNumber, int quantity, Date prodactionDate, Date expiryDate, int minTemperature, int maxTemperature, int price)
{
this._name = name;
this._catalogueNumber = catalogueNumber;
this._quantity = quantity;
this._price = price;
if(prodactionDate.before(expiryDate))
this._expiryDate = prodactionDate.tomorrow();
else
this._expiryDate = expiryDate;
if(minTemperature > maxTemperature)
{
this._minTemperature = maxTemperature;
this._maxTemperature = minTemperature;
}
else
{
this._minTemperature = minTemperature;
this._maxTemperature = maxTemperature;
}
}
最佳答案
当库存中的下一个商品更贵时,您正在创建一个新的 FoodItem,但您希望使最昂贵的FoodItem 成为库存中的下一个商品。
更改:
mostExpensiveFoodItem = new FoodItem(_stock[i]);
至
mostExpensiveFoodItem = _stock[i];
如果这不是错误,我不明白为什么代码是错误的,所以它可能在其他地方
编辑构造函数
用于复制 FoodItem 的构造函数不会设置价格,因此,如果您创建一个新的 FoodItem,则价格等于 null,因此第一个 FoodItem 始终具有最高价格,因为它是您定期创建的唯一一个。
更改构造函数:
public FoodItem(FoodItem otherFoodItem)
{
this._name = otherFoodItem.getName();
this._catalogueNumber = otherFoodItem.getCatalogueNumber();
this._quantity = otherFoodItem.getQuantity();
this._prodactionDate = otherFoodItem.getProdactionDate();
this._expiryDate = otherFoodItem.getExpiryDate();
this._minTemperature = otherFoodItem.getMinTemperature();
this._maxTemperature = otherFoodItem.getMaxTemperature();
//ADD THIS
this._price = otherFoodItem.getPrice();
}
关于java - 在java中的项目数组中查找最昂贵的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57921639/