Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么我的代码给出错误的输出?_Java - Fatal编程技术网

Java 为什么我的代码给出错误的输出?

Java 为什么我的代码给出错误的输出?,java,Java,我正在尝试为我的CS类编写一个模拟自动售货机操作的程序。我有一个双数组stock,表示特定“插槽”中的物品数量[我的自动售货机很奇怪,有点像一个长自动售货机,有一列不同的物品]。以下是我目前的代码: public class VendingMachine { // define fields here public static double itemPrice[]; public static String[] itemName; public static

我正在尝试为我的CS类编写一个模拟自动售货机操作的程序。我有一个双数组stock,表示特定“插槽”中的物品数量[我的自动售货机很奇怪,有点像一个长自动售货机,有一列不同的物品]。以下是我目前的代码:

 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][1];

        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 Quantity = quantity;

        for(int i = 0; i < itemName.length;i++){
            if (Quantity > (maxPerSlot-stock[i][0])){
            return;
            }
            if (Product.equals(itemName[i])&&Quantity < maxPerSlot){
                    stock[i][0] += Quantity;


            }else if ((maxPerSlot-stock[i][0])==0){

                continue;

            }
        }

        //Put # of products in slot that holds it and if that slot is full put the rest in the next 
        //available 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][0]; // 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 += this.getQuantity(i);
            }
        }
        return total;
    }

    public boolean buyItem(int slot)
    {   int snum = slot;
        double price = 0;
        if (stock[snum][0] != 0){
            stock[snum][0]--;
        price= itemPrice[snum];
        cashAmmount += price;
        return true;
        } else {
        return false;}
        // replace this line with your code
    }
}
当我运行此想法时,我始终得到:

true
5.75
8
15 
当我打算得到以下信息时,我的回答是:

true
5.75
9
12
作为我的输出。为什么会这样?我假设它与restockProduct()方法有关,但我似乎无法缩小范围,这真的让我很紧张。根据我的CS老师的说法,restockProduct()方法假设将指定数量的产品添加到自动售货机中,并将尽可能多的物品放入指定用于存放该特定类型产品的第一个插槽(使用setProduct())


如果不是所有的项目都能放入第一个插槽,那么将尽可能多的剩余项目放入第二个插槽,以容纳该类产品,等等。对于部分积分,您的方法至少应该能够找到指定产品的第一个插槽,并将所有项目放入其中“

你是对的,重新进货的产品并没有达到你想要的效果。以下是您所拥有的:

public void restockProduct(String product, int quantity)
{   
    String Product = product;
    int Quantity = quantity;

    for(int i = 0; i < itemName.length;i++){
        if (Quantity > (maxPerSlot-stock[i][0])){
        return;
        }
        if (Product.equals(itemName[i])&&Quantity < maxPerSlot){
                stock[i][0] += Quantity;


        }else if ((maxPerSlot-stock[i][0])==0){

            continue;

        }
    }
public void补货产品(字符串产品,整数数量)
{   
字符串产品=产品;
整数数量=数量;
for(int i=0;i(maxPerSlot库存[i][0])){
返回;
}
if(产品等于(项目名称[i])&数量
因此,当您第一次重新进货时,会发生以下情况:

  • 在循环0中,您是俗气的便便,所以您在其中放置了8项
  • 在循环1中,您使用了错误的类型,因此没有任何内容
  • 在循环2中,你有奶酪便便,所以你在那里放了8个
  • 不知何故,您需要记住您已将8放入第一个插槽。此外,您需要有一种机制将一些放入一个插槽,另一些放入另一个插槽,目前我认为在您的代码中不可能做到这一点。

    您的问题在于:

    for(int i = 0; i < itemName.length;i++){
        if (Quantity > (maxPerSlot-stock[i][0])){
            return;
    }
    
    将8件物品放入机器

    您第二次呼吁补充奶酪便便:

    v.restockProduct("Cheesy Poofs", 5);
    
    什么都做不到。您的if语句表示,如果数量(5)大于maxPerSlot-当前库存(10-8)或仅2,则返回

    5大于2,因此该方法结束,并且不会向计算机添加任何内容

    此外,一旦你将所有8个项目添加到机器中,你需要在其中加入某种控制来打破循环。目前,你正在将8个奶酪便便添加到两个不同的插槽中。一旦你将8个奶酪便便添加到第一个奶酪便便行中,你应该从剩下的库存中删除8个

    我冒昧地重建了这种方法,我想这就是你想要实现的:

    public void restockProduct(String product, int quantity)
    {   
        String Product = product;
        int Quantity = quantity;
    
        for(int i = 0; i < itemName.length;i++){
    
            if (Product.equals(itemName[i])){
    
                if (Quantity > (maxPerSlot-stock[i][0])){
                    Quantity -= maxPerSlot-stock[i][0];
                    stock[i][0] += maxPerSlot-stock[i][0];
    
                }
                if (Quantity <= (maxPerSlot-stock[i][0])){
                    stock[i][0] += Quantity;
                    return;
                }
            }
        }
    }
    
    public void补货产品(字符串产品,整数数量)
    {   
    字符串产品=产品;
    整数数量=数量;
    for(int i=0;i(maxPerSlot库存[i][0])){
    数量-=maxPerSlot库存[i][0];
    股票[i][0]+=maxPerSlot股票[i][0];
    }
    
    如果(数量其他人描述的再进货产品中存在多个问题,无论如何,您可以将再进货产品功能更改为此功能:

    public void restockProduct(final String product, int quantity)
    {
        for (int i = 0; i < itemName.length; i++)
        {
            if ( (product.equals(itemName[i]) || "".equals(product) ) && (maxPerSlot - stock[i][0]) > 0)
            {
                stock[i][0] += quantity;
                if (stock[i][0] > maxPerSlot)
                {
                    quantity = stock[i][0] - maxPerSlot;
                    stock[i][0] = maxPerSlot;
                }
                else
                    return;
            }
        }
    
        if (quantity > 0)
            throw new IllegalArgumentException("Cannot stock product");
    }
    
    public void补货产品(最终字符串产品,整数数量)
    {
    for(int i=0;i0)
    {
    库存[i][0]+=数量;
    if(库存[i][0]>maxPerSlot)
    {
    数量=库存[i][0]-maxPerSlot;
    股票[i][0]=maxPerSlot;
    }
    其他的
    返回;
    }
    }
    如果(数量>0)
    抛出新的IllegalArgumentException(“无法储存产品”);
    }
    
    注意:我们要么将产品插入到已经有产品的插槽中,要么在根本没有产品的插槽中
    注2:插入后,我们正在检查是否有其他内容需要进一步插入或所有内容都在这里

    您的重新进货方法所做的与它应该做的描述不相似。建议您将其废弃并重新开始。为什么它应该打印9?插槽2中应该有3个项目
    public void restockProduct(String product, int quantity)
    {   
        String Product = product;
        int Quantity = quantity;
    
        for(int i = 0; i < itemName.length;i++){
    
            if (Product.equals(itemName[i])){
    
                if (Quantity > (maxPerSlot-stock[i][0])){
                    Quantity -= maxPerSlot-stock[i][0];
                    stock[i][0] += maxPerSlot-stock[i][0];
    
                }
                if (Quantity <= (maxPerSlot-stock[i][0])){
                    stock[i][0] += Quantity;
                    return;
                }
            }
        }
    }
    
    public void restockProduct(final String product, int quantity)
    {
        for (int i = 0; i < itemName.length; i++)
        {
            if ( (product.equals(itemName[i]) || "".equals(product) ) && (maxPerSlot - stock[i][0]) > 0)
            {
                stock[i][0] += quantity;
                if (stock[i][0] > maxPerSlot)
                {
                    quantity = stock[i][0] - maxPerSlot;
                    stock[i][0] = maxPerSlot;
                }
                else
                    return;
            }
        }
    
        if (quantity > 0)
            throw new IllegalArgumentException("Cannot stock product");
    }