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

Here is my code for converting Centimeter to Inches and Grams to Pounds.

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 calculation results me always Zero. Am i doing the Math.sqrt rightly inside BSA.

4

3 回答 3

3

制作方法calculateBSA&convertGramsToPounds返回double而不是int. 由于 yourdouble gramsToPoundUnit = .00220462262;小于 1,因此转换为int正在返回0,这导致了问题。

此外,由于那里gramsToPoundUnitgramscalculateBSA很小(

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

calculateBSA(103.2, 5000.4) 给出 0.37487355474941564

于 2013-04-25T11:24:52.440 回答
1

如果你打电话

计算BSA(1e4, 1e4)

它返回 5。对于相对较小的克值,convertGramsToPounds(double Gram) 返回 0,因为您将其转换为 int。类似的情况发生在 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;
}
于 2013-04-25T11:36:18.047 回答
1

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

于 2013-04-25T11:24:26.240 回答