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