我似乎无法获得这组代码以在CubicCent上返回双精度答案。我知道将1加仑转换为立方厘米的确切答案应该是3784.41178,但是它始终返回不带小数的位置,而我的gs函数仅返回一个小数。提前致谢。
public class Volume {
public static final double Liters_Per_Gallon = 3.785;
public static final double Cubic_Cent_Per_Liter = 1000.0;
public static final double Ounces_Per_Pound = 16.0;
public static final double Grams_Per_Ounce = 28.35;
private double pounds;
private double gallons;
double grams;
double CubicCent;
/**
* @return the gallons
*/
public double getGallons() {
return gallons;
}
/**
* @param gallons
* the gallons to set
*/
public void setGallons(double gallons) {
this.gallons = gallons;
}
/**
* @return the pounds
*/
public double getPounds() {
return pounds;
}
/**
* @param pounds
* the pounds to set
*/
public void setPounds(double pounds) {
this.pounds = pounds;
}
public double getCubicCent() {
double liters = gallons * Liters_Per_Gallon;
CubicCent = liters * Cubic_Cent_Per_Liter;
return CubicCent;
}
public double getGrams() {
double ounces = pounds * Ounces_Per_Pound;
grams = ounces * Grams_Per_Ounce;
return grams;
}
为了获得想要的答案,您需要更加准确的Liters_Per_Gallon
初始化。例如,使用
public static final double Liters_Per_Gallon = 3.785411784;
结果为3785.411784。您将获得两倍于精度的极限,即恒定值的1000倍。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句