2016-09-19 68 views
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());我得到它說,實際的和正式的參數列表長度不同的錯誤。

+1

這只是因爲你的實際和正式參數列表長度不同。儘管如此,爲什麼你有'getSubtotal'函數接受一個什麼都不做的論據?擺脫它,你的電話將工作。 –

+0

當然,只需更新你的方法:'public double getSubtotal(){return subTotal; }並且編譯器會得到滿足。 – DimaSan

回答

1

你會得到這個錯誤,因爲該方法需要傳入一個double,但是你沒有參數調用它。

你可以改變你的getSubtotal方法,看起來像這樣和補充後,它會簡單的返回你的大部變量的值:

public double getSubtotal() { 
    return subTotal; 
} 

這應該給你你想要的結果!

+0

非常感謝!你是一個拯救生命的人:) – user5814640

相關問題