0
私は人のBMIを計算するプログラムを書いています。BMIの計算と浮動小数点の丸めを防ぐ方法(Java)
"体格指数(BMI)は、体重の体重をキログラムで表し、身長をメートルで2乗して計算することができます。プログラムは、体重Wをインチと高さHで入力するよう促し、BMIを表示します.1ポンドは0.45359237キログラム、1インチは0.0254メートルです。
入力:50 200内(ライン1)実数(ライン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);
}
}