*2.14(健康应用:计算BMI)身体质量指数(BMI)是对体重的健康测量。它的值可以通过将体重(以千克为单位)除以身高(以米为单位)的平方得到。编写程序,提示用户输入体重(以磅为单位)以及身高(以英寸为单位),然后显示BMI。注意:1磅是0.45359237千克,1英寸是0.0254米。
下面是一个运行示例:
Enter weight in pounds : 95.5
Enter height in inches : 50
BMI is 26.8573
*2.14(Health application: computing BMI) Body Mass Index (BMI) is a measure of health on weight. It can be calculated by taking your weight in kilograms and dividing by the square of your height in meters. Write a program that prompts the user to enter a weight in pounds and height in inches and displays the BMI. Note one pound is 0.45359237 kilograms and one inch is 0.0254 meters.
Here is a sample run:
Enter weight in pounds : 95.5
Enter height in inches : 50
BMI is 26.8573
下面是参考答案代码:
import java.util.*;
public class ComputingBMIQuestion14 {
public static void main(String[] args) {
double Pounds,Inches,Kilograms,Meters,BMI;
System.out.print("Enter weight in pounds : ");
Scanner PoundInput = new Scanner(System.in);
Pounds = PoundInput.nextDouble();
System.out.print("Enter height in inches : ");
Scanner InchInput = new Scanner(System.in);
Inches = InchInput.nextDouble();
Kilograms = Pounds * 0.45359237;
Meters = Inches * 0.0254;
BMI = Kilograms / Math.pow(Meters, 2);
System.out.println("BMI is " + BMI);
}
}
运行效果:
注:编写程序要养成良好习惯
如:1.文件名要用英文,具体一点
2.注释要英文
3.变量命名要具体,不要抽象(如:a,b,c等等),形式要驼峰化
4.整体书写风格要统一(不要这里是驼峰,那里是下划线,这里的逻辑段落空三行,那里相同的逻辑段落空5行等等)