gpt4 book ai didi

java - 错误: Actual and formal argument lists differ in length?

转载 作者:行者123 更新时间:2023-12-01 09:29:17 26 4
gpt4 key购买 nike

我正在尝试完成购物车建模类(class)。

这是我的代码:

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()); 我收到一个错误,指出实际参数列表和形式参数列表的长度不同

最佳答案

您收到此错误是因为该方法需要传入 double 值,但您在不带参数的情况下调用它。

您可以将 getSubtotal 方法更改为如下所示,它只会在添加后返回小计变量的值:

public double getSubtotal() {
return subTotal;
}

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

关于java - 错误: Actual and formal argument lists differ in length?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39580680/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com