我的代码是这样,但是我有一个问题,就是要卖出很多。我不想为lotNumber输入一个int,因为我想显示所有未售出的手。
我哪里做错了?
public ArrayList<Lot> getUnsold()
{
for(Lot lot : lots)
{
Bid highestBid = lot.getHighestBid();
lotNumber = lot.getNumber();
Unsold = new ArrayList<Lot>();
if (highestBid != null)
{
System.out.println("Lot number " + lotNumber + " is sold"); //retuern "Sold" is highestBid
}
else
{
System.out.println(lotNumber); //print bidder and highest bid value
}
}
return Unsold;
}
最佳答案
您需要在for循环之前实例化Unsold
(应该未售出)。并且您没有在Unsold
块的else
列表中添加任何内容。您需要将该批次添加到Unsold
块中的else
。
//assuming you are passing `lots` as parameter
public List<Lot> getUnsold(List<Lot> lots)
{
List<Lot> unsold = new ArrayList<Lot>();
for(Lot lot : lots)
{
Bid highestBid = lot.getHighestBid();
lotNumber = lot.getNumber();
if (highestBid != null)
{
System.out.println("Lot number " + lotNumber + " is sold"); //retuern "Sold" is highestBid
}
else
{
System.out.println(lotNumber); //print bidder and highest bid value
unsold.add(lot); // you are missing this
}
}
return unsold;
}