让用户输入重量,英尺和英寸。
Exmaple:一个人身高是5英尺10英寸,输入的英尺值就是5,英寸值为10
import java.util.Scanner;
public class ComputeAndInterpretBMI {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// Prompt the user to enter weight in pounds
System.out.print("Enter weight in pounds: ");
double weight = input.nextDouble();
// Prompt the user to enter height in inches
System.out.print("Enter feet: ");
double heightFeet = input.nextDouble();
System.out.print("Enter inches :");
double heightInche = input.nextDouble();
final double KILOGRAMS_PER_POUND = 0.45359237; // Constant 1磅=0.45359237米
final double METERS_PER_INCH = 0.0254; // Constant 1英寸=0.0254米
final double METERS_PER_FOOT = 0.3048;//Constant 1英尺=12英寸=0.3048米
// Compute BMI
double weightInKilograms = weight * KILOGRAMS_PER_POUND; //英镑的单位转换
double heightInMeters = heightFeet * METERS_PER_FOOT + heightInche * METERS_PER_INCH; //英尺+英寸的单位转换
double bmi = weightInKilograms / (heightInMeters * heightInMeters);//BMI计算
// Display result
System.out.println("BMI is " + bmi);
if (bmi < 18.5)
System.out.println("Underweight");
else if (bmi < 25)
System.out.println("Normal");
else if (bmi < 30)
System.out.println("Overweight");
else
System.out.println("Obese");
}
}