2013-12-11 106 views
1

我正試圖編寫一個程序來模擬CS類的自動售貨機的操作。我有一個雙數組庫存,它表示特定「插槽」中的物品數量[我的自動售貨機很奇怪,有點像一臺自動售貨機,有一列不同的物品]。這是我到目前爲止的代碼:爲什麼我繼續得到ArrayIndexOutofBoundsException?

public class VendingMachine 
{ 
    // define fields here 
    public static double itemPrice[]; 
    public static String[] itemName; 
    public static int stock[][]; 
    public static int maxPerSlot; 
    public static double cashAmmount; 

    public VendingMachine(int numslots, int maxperslot, double cash) 
    { 
     final int numSlots = numslots; 
     maxPerSlot = maxperslot; 
     cashAmmount = cash; 
     stock = new int[numSlots][0]; 

     itemPrice = new double[numSlots]; 
     itemName = new String[numSlots]; 

     // complete this method 
    } 

    public void setProduct(int slot, String product, double price) 
    { int Slot = slot; 
     itemPrice[slot] = price; 
     itemName[slot] = product; 
     stock[Slot][0] = 0; 

     // 
    } 

    public void restockProduct(String product, int quantity) 
    { 
     String Product = product; 
     int currentCapacity = quantity - maxPerSlot; 
     for(int i = 0; i < stock.length; i++){ 
      if (itemName[i]==Product){ 
       for(;quantity <= maxPerSlot && currentCapacity != 0; quantity--) 
       stock[i][0] += 1; 
      } 
     } 


     //Put # of products in slot that holds it and if that slot is full put the rest in the next 
     //availble slot that holds that product, if all full return error. 
    } 

    public double getCashOnHand() 
    { 
     return cashAmmount; // replace this line with your code 
    } 

    public int getQuantity(int slot) 
    { 
     return stock[slot][1]; // replace this line with your code 
    } 

    public int getQuantity(String product) 
    { int total = 0; 

     for (int i = 0; i<itemName.length;i++){ 
      if (product == itemName[i]){ 
       total += stock[i][1]; 
      } 
     } 
     return total; 
    } 

    public boolean buyItem(int slot) 
    { int snum = slot; 
     if (stock[snum][1] != 0){ 
      stock[snum][1]--; 
     return true; 
     } else { 
     return false;} // replace this line with your code 
    } 
} 

每次我在線程 「主要」 java.lang.ArrayIndexOutOfBoundsException runException:0 在VendingMachine.setProduct(VendingMachine.java:27) 在vmd.main(VMD。 java:9)這段代碼雖然我得到這個錯誤信息:

有人可以在這裏請向我解釋爲什麼我繼續得到這個錯誤?我的意思是邏輯看起來非常正確。

+0

所以你有一個錯誤來自你的主函數,但你不打算將它包含在你的代碼中? –

回答

1

你的問題是在這裏:

stock = new int[numSlots][0]; 

這定義numSlot數組的數組具有的每個0的長度。

0

當您試圖索引零訪問元素你分配在stock第二維零個元素,

stock = new int[numSlots][0]; 

,所以你得到那個例外。

stock[Slot][0] = 0; 
0

此行

stock = new int[numSlots][0]; // <-- A length of zero? You want a one there. 

應該

stock = new int[numSlots][1]; // <-- like so. Or, if you really don't 
      // want to change your other code make it a 2. 
      // But you'll leave memory unused, and you really should change it. 

在其他地方(你有這樣的代碼) -

stock[slot][1] // <-- stock[INDEX][1] <--- should be 0. 

像這樣

stock[slot][0] // <-- all of the other accesses. 
0

因爲該行:

stock = new int[numSlots][0]; 

分配stock是一個數組的數組,每個這些陣列的長度爲0,所以,你不能分配任何東西到這些陣列(他們沒有任何要分配的元素)。所以,當你這樣做:

stock[Slot][0] = 0; 

你會得到一個ArrayIndexOutOfBounds。請記住,在Java中,索引從0開始,因此如果需要索引從0到N的數組,則必須分配大小爲N + 1的數組。

1

,當你在構造函數初始化股票做到這一點,而不是:

stock = new int[numSlots][1]; 

使用0而不是1初始化一個長度爲0的數組!

相關問題