Java语言程序设计(基础篇)第十版 编程练习题3.6(医疗应用程序:BMI)

让用户输入重量,英尺和英寸。
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");
  }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值