简体   繁体   English

变量未根据其他更改进行更新

[英]Variable not updating based on a change of another

In my program I have a method calculateCost(), which gets the cost of a truck based on the minimum temperature of said truck. 在我的程序中,我有一个方法calculateCost(),该方法根据卡车的最低温度获取卡车的成本。

public double calculateCost() {

    int minimumTemperature = this.getTemperature();

    System.out.println("Temp is: " + minimumTemperature);

    double costOfTruck = 900 + 200 * (Math.pow(0.7, (minimumTemperature / 5))); 

    System.out.println("Cost is: " + costOfTruck);

    return costOfTruck;
}

When this method is executed, the minimumTemperature correctly changes as shown in the console, however, the costOfTruck doesn't change when the minimumTemperature is changed. 当执行此方法时,minimumTemperature正确更改,如控制台中所示,但是,更改minimumTemperature时costOfTruck不会更改。

如果minimumTemperature <5,则由于整数除法, minimumTemperature / 5将等于零,而Math.pow(0.7, (minimumTemperature / 5)将等于1,因此请尝试使用双数值类型

double costOfTruck = 900 + 200 * (Math.pow(0.7, (minimumTemperature / 5.0)));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM