2016-09-19 30 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

相关问题