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