所以我做了一些作业,其中有一个FoodItem类和一个Stock类。在“股票”类中,有一个FoodItem_stock)数组和该数组中当前位置的整数(_noOfFoodItems)。

一项分配是找到数组中最昂贵的商品(FoodItem的类属性之一是int _price)。另一个指令是,如果数组为空,则该方法应返回null。我已经写了每个属性getset方法。

起初,在我看来,就像其他任何“在数组中找到最大的项目”一样,但是由于某种原因,我似乎对此感到困惑。到目前为止,这是我得出的结果:

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,但您想使mostExpensiveFoodItem成为库存中的下一个商品。

更改:

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();
     }

10-04 17:03