0
我正在尝试完成模拟购物车的课程。错误:实际和正式参数列表的长度不同?
这里是我的代码:
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());
我得到它说,实际的和正式的参数列表长度不同的错误。
这只是因为你的实际和正式参数列表长度不同。尽管如此,为什么你有'getSubtotal'函数接受一个什么都不做的论据?摆脱它,你的电话将工作。 –
当然,只需更新你的方法:'public double getSubtotal(){return subTotal; }并且编译器会得到满足。 – DimaSan