gpt4 book ai didi

Java如何使参数成为另外两个参数的乘积?

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

我确信这是一个菜鸟问题,但在我正在处理的程序中,我试图传递一个变量作为参数,这很容易,但我被困住了,因为我的变量尝试通过是之前两个参数的产物。

我必须为机动车辆创建一个对象,其中包含车主姓名、车轮数量和排量(升)参数,但第四个也是最后一个参数是排量与数量相乘的结果车轮组合在一起,称为“马力”(尽管我很确定这不是马力的真正计算方式。)

无论如何,在定义“MotorizedVehicle”的类中,我有以下代码。

    /*


public class MotorizedVehicle extends Bicycle {
int litersDisplaced;
int horsepower = litersDisplaced * wheelCount;




public MotorizedVehicle(String ownerName, int wheelCount, int litersDisplaced, int horsepower){
this.ownerName = ownerName;
this.wheelCount = wheelCount;
this.litersDisplaced = litersDisplaced;
this.horsepower = horsepower;

}

public int getLitersDisplaced() {
return litersDisplaced;
}

public void setLitersDisplaced(int litersDisplaced) {
this.litersDisplaced = litersDisplaced;
}

public int getHorsepower() {
return horsepower;
}

public void setHorsepower(int horsepower) {
this.horsepower = horsepower;
}
}

现在,在客户端类中我有以下代码:

/*

package extracredit4;

/**
*
* @author ChetSpalsky
*/
public class VehicleClient {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
MotorizedVehicle car1 = new MotorizedVehicle("Bill", 2, 4, *); //Where I have the * this is where the argument for the "horsepower" variable or method should be, but I don't know what to put here to call the variable that will be litersDisplaced * wheelCount.


}

}

需要明确的是,我不能只将“8”放在第四个参数应该所在的位置,我必须调用将literDisplaced 和wheelCount 相乘的方法。

很抱歉,如果这是一个会让经验丰富的程序员捂脸的问题,但我已经查遍了,似乎找不到这个问题的答案,我真的很感谢你的帮助。谢谢!

最佳答案

只需输入 2 * 4,如 MotorizedVehicle car1 = new MotorizedVehicle("Bill", 2, 4, 2 * 4);

关于Java如何使参数成为另外两个参数的乘积?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36950622/

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