*3.6(Health application: BMI) Revise Listing 3.4, ComputeAndInterpretBMI,java, to let the user enter weight, feet, and inches.For example, if a person is 5 feet and 10inches, you will enter 5 for feet and 10 for inches. Here is a sample run:
Enter weight in pounds:140
Enter feet:5
Enter inches:10
BMI is 20.087702275404553
Normal
*3.6(健康指标:BMI)修改清单3.4,ComputeAndInterpretBMI,java,让用户输入体重、英尺和英寸。例如,如果某人的身高为5英尺10英寸,则输入5表示英尺,10表示英寸。下面是一个示例运行:
Enter weight in pounds:140
Enter feet:5
Enter inches:10 BMI is 20.087702275404553 Normal
代码如下:
import java.util.Scanner;
public class Unite3Test6 {
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Enter weight in pounds:");
double weight =scan.nextDouble();
System.out.println("Enter inches:");
double inches =scan.nextDouble();
System.out.println("Enter feet:");
double feet =scan.nextDouble();
double height=12*feet+inches;
final double KILOGRAMS_PER_POUND=0.45359237;
final double METERS_PER_INCH=0.0254;
double weightInKilograms=weight*KILOGRAMS_PER_POUND;
double heightInMeters=height*METERS_PER_INCH;
double bmi=weightInKilograms/(heightInMeters*heightInMeters);
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");
}
}
结果如下