1

我的代码是这样的,但是我遇到了未售出的问题。我不想为 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;
}
4

1 回答 1

6

您需要Unsold在 for 循环之前实例化(应该未售出)。而且您没有在块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;
    }
于 2013-03-05T21:21:44.030 回答