BSA计算结果我归零

时间:2013-04-25 11:15:38

标签: java

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

以下是我将Centimeter转换为英寸和Grams转换为磅的代码。

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。

3 个答案:

答案 0 :(得分:3)

制作方法calculateBSA& convertGramsToPounds返回double而非int。由于您的double gramsToPoundUnit = .00220462262;小于1,因此转化为int正在返回0,这会导致问题。

此外,由于您的值很小(gramsToPoundUnit),因此传递给grams的{​​{1}}值最好是一个很大的值,以获得一些正确的结果!

例如: - 完成上述变更后,

  

calculateBSA(103.2,5000.4)给出0.37487355474941564

答案 1 :(得分:1)

如果要转换为整数的任何一个计算小于1,那么结果将为零。

答案 2 :(得分:1)

如果你打电话

  

calculateBSA(1e4,1e4)

它返回5.对于相对较小的克数,convertGramsToPounds(double gram)返回0,因为您将它转换为int。与calculateBSA和convertCentimeterToInches方法类似的情况也是如此。如果您可以接受double值,可以将代码修改为:

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