计算总是等于 0
Calculations always equal 0
我有这段代码,每当我输入任何值时,结果始终为 0。我在每个 if 语句中设置断点,计算中使用的值始终有效,但限制仍然存在
可能还值得注意的是 soilDepth
和 rnv
是整数。为了以防万一,我尝试将它们转换为双打,但没有任何改变。
final TextView limingTV = (TextView) findViewById(R.id.limingText);
double liming;
if (targetPh == 6.8) {
liming = (71.4 - 1.03 * bufferpH * 10) * (soilDepth / 8) * (65 / rnv);
} else if (targetPh == 6.5) {
liming = (60.4 - .87 * bufferpH * 10) * (soilDepth / 8) * (65 / rnv);
} else if (targetPh == 6.0) {
liming = (49.3 - .71 * bufferpH * 10) * (soilDepth / 8) * (65 / rnv);
} else { //If 6.8 is left as default on drop down menu its not passed
liming = (71.4 - 1.03 * bufferpH * 10) * (soilDepth / 8) * (65 / rnv);
}
limingTV.setText(String.format("%.4f lbs/acre", liming));
整数除法可能导致 0 值。
尝试类似 -
double result = ((double)x) / y;
您需要将 soilDepth 和 rnv 中的一个或两个转换为双精度值。
要添加更多,您也可以试试这个(不转换)
double result = x * 1.0/y;
如 Bhush_Techidiot 所说,在数学开始前转换为 double。 Div 使用整数并不像您期望的那样工作。
final TextView limingTV = (TextView) findViewById(R.id.limingText);
double liming;
double dblSoilDepth = (double) soilDepth;
double dblRnv = (double) rnv;
if (targetPh == 6.8) {
liming = (71.4 - 1.03 * bufferpH * 10) * (dblSoilDepth / 8) * (65 / dblRnv);
} else if (targetPh == 6.5) {
liming = (60.4 - .87 * bufferpH * 10) * (dblSoilDepth / 8) * (65 / dblRnv);
} else if (targetPh == 6.0) {
liming = (49.3 - .71 * bufferpH * 10) * (dblSoilDepth / 8) * (65 / dblRnv);
} else { //If 6.8 is left as default on drop down menu its not passed
liming = (71.4 - 1.03 * bufferpH * 10) * (dblSoilDepth / 8) * (65 / dblRnv);
}
limingTV.setText(String.format("%.4f lbs/acre", liming));
在计算结果之前尝试将 int
转换为 double
。
double newSoilDepth = (double) soilDepth;
double newRnv = (double) rnv;
然后在计算中使用新的双打。这可以导致更清晰的代码。
我有这段代码,每当我输入任何值时,结果始终为 0。我在每个 if 语句中设置断点,计算中使用的值始终有效,但限制仍然存在
可能还值得注意的是 soilDepth
和 rnv
是整数。为了以防万一,我尝试将它们转换为双打,但没有任何改变。
final TextView limingTV = (TextView) findViewById(R.id.limingText);
double liming;
if (targetPh == 6.8) {
liming = (71.4 - 1.03 * bufferpH * 10) * (soilDepth / 8) * (65 / rnv);
} else if (targetPh == 6.5) {
liming = (60.4 - .87 * bufferpH * 10) * (soilDepth / 8) * (65 / rnv);
} else if (targetPh == 6.0) {
liming = (49.3 - .71 * bufferpH * 10) * (soilDepth / 8) * (65 / rnv);
} else { //If 6.8 is left as default on drop down menu its not passed
liming = (71.4 - 1.03 * bufferpH * 10) * (soilDepth / 8) * (65 / rnv);
}
limingTV.setText(String.format("%.4f lbs/acre", liming));
整数除法可能导致 0 值。
尝试类似 -
double result = ((double)x) / y;
您需要将 soilDepth 和 rnv 中的一个或两个转换为双精度值。
要添加更多,您也可以试试这个(不转换)
double result = x * 1.0/y;
如 Bhush_Techidiot 所说,在数学开始前转换为 double。 Div 使用整数并不像您期望的那样工作。
final TextView limingTV = (TextView) findViewById(R.id.limingText);
double liming;
double dblSoilDepth = (double) soilDepth;
double dblRnv = (double) rnv;
if (targetPh == 6.8) {
liming = (71.4 - 1.03 * bufferpH * 10) * (dblSoilDepth / 8) * (65 / dblRnv);
} else if (targetPh == 6.5) {
liming = (60.4 - .87 * bufferpH * 10) * (dblSoilDepth / 8) * (65 / dblRnv);
} else if (targetPh == 6.0) {
liming = (49.3 - .71 * bufferpH * 10) * (dblSoilDepth / 8) * (65 / dblRnv);
} else { //If 6.8 is left as default on drop down menu its not passed
liming = (71.4 - 1.03 * bufferpH * 10) * (dblSoilDepth / 8) * (65 / dblRnv);
}
limingTV.setText(String.format("%.4f lbs/acre", liming));
在计算结果之前尝试将 int
转换为 double
。
double newSoilDepth = (double) soilDepth;
double newRnv = (double) rnv;
然后在计算中使用新的双打。这可以导致更清晰的代码。