0
我正在编写计算一个人的BMI的程序。这是我给予的任务:计算BMI以及如何防止向上舍入浮点(Java)
“身体质量指数(BMI)是衡量健康与体重的指标,可以通过以千克为单位计算体重并除以身高的平方米来计算。该程序提示用户输入体重W以英寸为单位,身高H以英寸输入,并显示BMI。请注意,一磅为0.45359237公斤,一英寸为0.0254米。
输入:(1号线),以在50实数200 (第2行)实数在10至100
输出:BMI值(浮点应该只被打印,直到第二小数点)
问题是,无论何时使用“System.out.printf(”%。2f \ n“,BMI)”“,输出都被舍入,而不是切断小数点的其余部分。这是我的代码:
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
double weight = input.nextDouble();
double height = input.nextDouble();
double weightKG;
double heightM;
double heightMSquare;
double BMI;
final double kilogram = 0.45359237;
final double meter = 0.0254;
while ((weight > 200) || (weight < 50)) // Error catching code.
{
weight = input.nextDouble();
}
while ((height > 100) || (height < 10))
{
height = input.nextDouble();
}
weightKG = weight * kilogram; // Convert pounds and inches to
kilograms and meters.
heightM = height * meter;
heightMSquare = Math.pow(heightM, 2); // Compute square of height in
meters.
BMI = weightKG/heightMSquare; // Calculate BMI by dividing weight
by height.
System.out.printf("%.2f\n", BMI);
}
}