设置为Double的常量不会返回double

时间:2015-06-20 14:45:24

标签: java

我似乎无法获得这组代码来返回CubicCent答案的双重代码。我知道1 Gallon到Cubic Centimeter的确切答案应该是3785.41178,但它不会返回,没有小数,我的克函数只返回一个小数。提前谢谢。

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;
}

1 个答案:

答案 0 :(得分:0)

要获得所需的答案,您需要在Liters_Per_Gallon初始化时更准确。例如,使用

public static final double Liters_Per_Gallon = 3.785411784;

结果为3785.411784。你将达到双倍精度的极限,是恒定值的1000倍。