试图获得未售出的手数

时间:2013-03-05 21:17:20

标签: java java-ee

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

1 个答案:

答案 0 :(得分:6)

您需要在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;
    }