栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java *3.6(医疗应用程序:BMI)修改程序清单3-4,让用户输入重量、英尺和英寸。例如:一个人身高是5英尺10英寸,输入的英尺值就是5、英寸值为10。

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java *3.6(医疗应用程序:BMI)修改程序清单3-4,让用户输入重量、英尺和英寸。例如:一个人身高是5英尺10英寸,输入的英尺值就是5、英寸值为10。

 下面是一个运行示例:

Enter weight in pounds:140
Enter height in feet5
Enter height in inches:10
BMI is 20.087702275404553
Normal

修改程序清单3-4

要点提示:嵌入if语句来编写程序,计算身体质量指数。

身体质量指数(BMI)是关于身体指标的健康测量。可以通过以千克为单位的体重除以以迷为单位的身高的平方,得到BMI的值。针对20岁及以上的人群,他们的BMI值得说明表如下所示:

BMI说明
BMI < 18.5 偏瘦
18.5 ≤ BMI < 25.0正常
25.0  ≤ BMI <  30.0超重
30.0  ≤ BMI 过胖

编写程序,提示用户输入以英镑为单位的体重,以及以英寸为单位的身高,然后显示BMI。注意:一磅是0.45359237千克,而一英寸是0.0254米。

程序清单3-4

package Chapter_03;

import java.util.Scanner;

public class Code_04 {
public static void main(String[] args) {
	
	Scanner input = new Scanner(System.in);
	System.out.print("Enter weight in pounds:");
	double weight = input.nextDouble();
	System.out.print("Enter height in inches:");
	double height = input.nextDouble();
	
	final double kilograms = 0.45359237;
	final double meters = 0.0254;
	
	double weightKilograms = weight * kilograms;
	double heightMeters = height * meters;
	double BMI = weightKilograms / (heightMeters * heightMeters);
	
	System.out.println("BMI is " + BMI);
	if(BMI < 18.5) 
		System.out.println("Underweight");
	else if(BMI < 25.0)
		System.out.println("Normal");
	else if(BMI < 30.0)
		System.out.println("Overweight");
	else
		System.out.println("Obese");
}
}

调试运行

Enter weight in pounds:140
Enter height in inches:70
BMI is 20.087702275404553
Normal

 本题3.6程序代码:

温馨提示:以英尺等于12英寸

package Chapter_03;

import java.util.Scanner;

public class Code_04 {
public static void main(String[] args) {
	
	Scanner input = new Scanner(System.in);
	System.out.print("Enter weight in pounds:");
	double weight = input.nextDouble();
	System.out.print("Enter height in feet");
	double feed = input.nextDouble();
	System.out.print("Enter height in inches:");
	double inches = input.nextDouble();
	
	final double weightKilograms = 0.45359237 * weight;
	final double heightMeters = 0.3048 * feed + 0.0254 *inches;

	double BMI = weightKilograms / (heightMeters * heightMeters);
	
	System.out.println("BMI is " + BMI);
	if(BMI < 18.5) 
		System.out.println("Underweight");
	else if(BMI < 25.0)
		System.out.println("Normal");
	else if(BMI < 30.0)
		System.out.println("Overweight");
	else
		System.out.println("Obese");
}
}

调试运行

Enter weight in pounds:140
Enter height in feet5
Enter height in inches:10
BMI is 20.087702275404553
Normal
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/924866.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号