gpt4 book ai didi

java - 谁能告诉我为什么它的计算结果为 0?

转载 作者:行者123 更新时间:2023-12-01 18:07:53 24 4
gpt4 key购买 nike

在 Java 类上工作,这让我发疯,因为这个表达式的计算结果为零,我需要它计算为 double,然后将其向下舍入到最接近的 int。所以我想要得到的是天数是整数天,但是当我通过java运行它时,它的计算结果为0。当我通过计算器运行它时,它的计算结果是正确的值。我想要一个修复程序并解释为什么我已经拥有的功能不起作用。

public int getEventDays(){
//variables
double daysCalc;
int days;

//logic
if (getStatus().equals("filling")){
//this is indented less to fit everything on one line, its not this way in
//the fractions are for unit conversion
daysCalc= Math.floor(((capacity-storage)/(inflow-outflow))*(43560)*(1/3600)*(1/24));
days = (int)daysCalc;

}
else if (getStatus().equals("emptying")){
//this is indented less to fit everything
//the fractions are for unit conversion
daysCalc=Math.floor(((storage-0)/(outflow-inflow))*(43560)*(1/3600)*(1/24));
days = (int)daysCalc;
}
else{
days = -1;
}

return days;


}

最佳答案

将您的代码更改为:

daysCalc = Math.floor(((storage-0)/(outflow-inflow))*(43560)*(1.0/3600)*(1.0/24));

说明:

右侧表达式返回一个整数值。在您的情况下,1/3600 四舍五入为 0,类似于 1/24 的情况。现在,通过使用 1.0 而不是 1,它给出了未舍入的浮点值 1/3600。

关于java - 谁能告诉我为什么它的计算结果为 0?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34890799/

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