2014-03-31 71 views
-1

在這個程序中,我想輸入假股票並通過股票代碼搜索它們,並打印出最後250個股票的平均成本。這裏是我到目前爲止,每當我輸入2,程序崩潰沒有任何錯誤信息。查找hashmap並平均arraylist中的對象的問題

package stocks; 

import java.util.*; 

public class Stocks { 
    private String sym; 
    private List<Purchase> purchases; 

    public Stocks(final String symbol) { 
     this.sym = symbol; 
     purchases = new ArrayList<Purchase>(); 
    } 

    public void addPurchase(final int amt, final double cost) { 
     purchases.add(new Purchase(amt, cost)); 
    } 

    public String getSym() { 
     return sym; 
    } 

    public void setSym() { 
     this.sym = sym; 
    } 

    public double getAvg250() { 
     int i = 0; 
     int total = 0; 
     int shares = 0; 
     while (i < purchases.size()) { 
      Purchase p = purchases.get(i); 
      if (shares + p.getAmt() >= 250) { 
       total += (250 - shares) * p.getCost(); 
       shares = 250; 
       break; 
      } 
      shares += p.getAmt(); 
      i++; 
     } 
     return total * 1.0/shares; 
    } 

    public class Purchase { 
     private int amt; 
     private int cost; 

     public Purchase(int amt, double cost) { 

     } 

     public int getAmt() { 
      return amt; 
     } 

     public void setAmt(int amt) { 
      this.amt = amt; 
     } 

     public int getCost() { 
      return cost; 
     } 

     public void setCost(int cost) { 
      this.cost = cost; 
     } 
    } 

    public static void main(String[] args) { 

     int choice = 0; 
     ArrayList<Stocks> StocksList = new ArrayList<Stocks>(); 
     Map<String, Stocks> stocks = new HashMap<String, Stocks>(); 

     while (choice == 0) { 
      System.out.println("Enter 1 to input a new stock, or 2 to query a stock's price, 3 to quit: "); 
      Scanner sc1 = new Scanner(System.in); 
      choice = sc1.nextInt(); 

      if (choice == 1) { 

       Scanner sc2 = new Scanner(System.in); 
       System.out.println("Please enter the stock symbol: "); 
       String sym = sc2.next(); 
       System.out.println("Please enter the number of shares: "); 
       int amt = sc2.nextInt(); 
       System.out.println("Please enter the price per share: "); 
       double cost = sc2.nextDouble(); 

       Stocks s = stocks.get(sym); 
       if (s == null) { 
        s = new Stocks(sym); 
        stocks.put(sym, s); 
       } 
       s.addPurchase(amt, cost); 

       choice = 0; 

       if (choice == 2) { 
        Scanner sc3 = new Scanner(System.in); 
        System.out.println("Please enter the symbol of the stock you wish to see: "); 
        String search = sc3.next(); 

        if (stocks.containsKey(search)) { 

         System.out.println(search + "'s LIFO price is " + s.getAvg250()); 
         choice = 0; 
        } 
       } 
       if (choice == 3) { 

       } 

      } 
     } 
    } 
} 
+0

你應該隔離你掙扎的一部分,並張貼,描述到底是什麼它你不能工作 - 發佈你的整個項目是不是真的符合Stackoverflow風格。 –

+0

現在,無論何時輸入2,程序都會關閉而沒有任何錯誤。我不確定該程序的哪一部分需要隔離。 – user2921899

+0

我的評論不是問題;我在說你應該清除你問題中不相關的噪音。 –

回答

0

您檢查用戶是否在if塊中輸入「2」來檢查用戶是否輸入了「1」。並且因爲在if (choice == 1)塊之後選擇仍然是2,所以程序將離開while循環。

應該是這樣的(如果你把這種編程方式)

while (choice == 0) { 
    // ... 
    if (choice == 1) { 
    } 
    if (choice == 2) { 
    } 
    if (choice == 3) { 
    break; 
    } 
    // ... 
    choice = 0 
} 

,而不是

while (choice == 0) { 
    // ... 
    if (choice == 1) { 

     choice = 0; 

     // choice 2 will never be true 
     if (choice == 2) { 
     } 

    } 
}