2013-04-25 150 views
0
public static int calculateBSA(double height, double grams) { 
    double weightforBmi = convertGramsToPounds(grams); 
    return (int) Math.sqrt(((convertCentimeterToInches(height) * weightforBmi)/3131)); 
} 

這裏是我的代碼轉換釐米英寸和克到磅。BSA計算結果我零

private static double convertCentimeterToInches(double height) { 
    return (Math.round((height/2.54) * 100)/100); 
} 

public static int convertGramsToPounds(double grams) { 
    double gramsToPoundUnit = .00220462262; 
    double pounds = (grams * gramsToPoundUnit); 
    return (int)(Math.round(pounds * 100)/100); 
} 

BSA計算結果我總是Zero。我是否正確地在BSA內部完成Math.sqrt。

回答

1

如果你打電話

calculateBSA(1E4,1E4)

它返回5.對於r由於您將它轉換爲int,克羅地亞convertGramsToPounds(雙克)的小值返回0。 calculateBSA和convertCentimeterToInches方法類似。如果您可以接受雙倍數值,可以將代碼修改爲:

public double calculateBSA(double height, double grams) { 
    double weightforBmi = convertGramsToPounds(grams); 
    return Math.sqrt(((convertCentimeterToInches(height) * weightforBmi)/3131)); 
} 

private double convertCentimeterToInches(double height) { 
    return (height/2.54); 
} 

public double convertGramsToPounds(double grams) { 
    double gramsToPoundUnit = .00220462262; 
    double pounds = (grams * gramsToPoundUnit); 
    return pounds; 
} 
1

如果其中一個計算結果小於1,則結果爲0。

3

使方法calculateBSA & convertGramsToPounds返回double而不是int。由於您的double gramsToPoundUnit = .00220462262;小於1,因此導致int正在返回0,這導致了問題。

另外,由於您的值很小(gramsToPoundUnit),所以您傳遞給calculateBSAgrams值會更好,以獲得一些正確的結果!

例如: - 經過上述改變完成,

calculateBSA(103.2,5000.4)給出0.37487355474941564