2013-12-11 111 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的数组!

相关问题