第三章第六题(健康应用:BMI)(Health application: BMI)

    科技2022-07-12  135

    第三章第六题(健康应用:BMI)(Health application: BMI)

    *3.6(健康应用:BMI)修改程序清单3-4,让用户输入重量、英尺和英寸。例如一个人身高是5英尺10英寸,输入的英尺值就是5、英寸值为10。注意:1英尺=0.3048米。 下面是一个运行示例: Enter weight in pounds:140 Enter feet:5 Enter inches:10 BMI is 20.087702275404553

    *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 10 inches, 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

    参考代码:

    package chapter03; import java.util.Scanner; public class Code_06 { 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 feet System.out.print("Enter feet: "); double feet = input.nextDouble(); // Prompt the user to enter inches System.out.print("Enter inches: "); double inches = input.nextDouble(); final double KILOGRAMS_PER_POUND = 0.45359237; // Constant final double METERS_PER_INCH = 0.0254; // Constant // Compute BMI double weightInKilograms = weight * KILOGRAMS_PER_POUND; double heightInMeters = (feet * 12 + inches) * METERS_PER_INCH; double bmi = weightInKilograms / (heightInMeters * heightInMeters); // 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 < 25) System.out.println("Overweight"); else System.out.println("Obese"); input.close(); } } 结果显示: Enter weight in pounds: 140 Enter feet: 5 Enter inches: 10 BMI is 20.087702275404553 Normal Process finished with exit code 0
    Processed: 0.013, SQL: 8