错误:实际和正式参数列表的长度不同?

时间:2016-09-19 19:29:57

标签: java

我正在尝试完成模拟购物车的课程。

这是我的代码:

public class ShoppingCart {
    private double price;
    private double subTotal;
    private double cart;

    /**
     * initializing variable named subTotal
     */
    public ShoppingCart() {
        subTotal = 0;
    }

    /**
     * adds this cost to the subtotal for this ShoppingCart
     *
     * @param addPrice Any double value that will be added
     */
    public void add(double addPrice) {
        subTotal = subTotal + addPrice;
    }

    /**
     * subtracts this cost from the subtotal for this ShoppingCart
     *
     * @param subtractPrice Any double value that will be subtracted
     */
    public void remove(double subtractPrice) {
        subTotal = subTotal - subtractPrice;
    }

    /**
     * gets the subtotal for this ShoppingCart
     *
     * @param totalCost Any double value that will be the total amount
     * @return the cost of things in ShoppingCart
     */
    public double getSubtotal(double totalCost) {
        totalCost = subTotal;
        return subTotal;
    }
}


public class ShoppingCartTester {
    public static void main(String[] args) {
        ShoppingCart cart = new ShoppingCart();
        cart.add(10.25);
        cart.add(1.75);
        cart.add(5.50);
        System.out.println(cart.getSubtotal());
        System.out.println("Expected: 17.5");
        cart.remove(5.50);
        cart.add(3);
        System.out.println(cart.getSubtotal());
        System.out.println("Expected: 15.0");
    }
}

System.out.println(cart.getSubtotal());我收到一条错误,指出实际和正式参数列表的长度不同

1 个答案:

答案 0 :(得分:1)

您收到此错误,因为该方法需要传入double,但您调用它时没有参数。

您可以将getSubtotal方法更改为这样,并且只会在添加后返回小计变量的值:

public double getSubtotal() {
    return subTotal;
}

这应该会给你你想要的结果!