目录
前言
思路讲解
代码展示
python
注意事项
Java
C
BMI全称叫Body Mass Index,也就是身体质量指数,讲的就是相对于你的身高,你的体重是否正常。这个程序非常简单且易上手,刚学编程不久的朋友都能够看懂。
我们编写这个程序的目的就是通过用户给到的他身高与体重的信息,去判断他的BMI是:underweight(体重过低), normal(体重正常), overweight(体重偏高), obese(肥胖)。
我们先给出BMI的公式:BMI=weight(千克为单位)/height^2(米为单位)。我们想要这个程序是这样子的:用户输入他的名字,我们跟他打个招呼,然后要求用户输入自己的体重和身高信息,算出bmi。bmi<20为体重过低,20<=bmi<25是体重正常,25<=bmi<30是体重偏高,bmi>=30是肥胖。但是如果我们的用户来自全世界各地的话,我们就会发现,很多外国人习惯使用的计量单位和我们使用的国际单位制(metric)不太一样,比如说英制(imperial)。因此,考虑到这些人的需求,我们需要在用户输入身高体重前判断用户是否想用英制还是国际单位制。既然单位改变了,公式就要跟着变。我们这里有两种思路。第一种是在用户输入英制后进行一个单位转换到国际单位制。第二种是改变公式,使其单位变成英制的:BMI=703*weight(磅为单位)/height^2(英尺为单位),这里我们两个都用。这个程序的流程图如下所示:
import os, time
def metric(weight,height):
return weight/(height**2)
def imperial(weight,height):
return weight*703/(height**2)
def function():
weight=input("enter your weight:")
height=input("enter your height:")
test1=weight.replace(".","")
test2 = height.replace(".","")
assert test1.isdigit() and test2.isdigit(),"no alphabet or negative number!"
weight=float(weight)
height=float(height)
print("thank you\n")
return [weight,height]
def BMI_1():
firstname=input("enter your first name:")
surname=input("enter your surname:")
print(f"hi {firstname}")
print("welcome to this health program")
time.sleep(0.5)//暂停程序0.5秒
while True:
choice=input("do you want to choose metric or imperial(press 0 to exit):")
if choice=="metric"://国际单位制计算
dataLst=function()
dataLst[1]/=100.0//由于用户输入的数据单位是厘米,这里需要进行一个转换
bmi=metric(dataLst[0],dataLst[1])
return round(bmi,1)
elif choice=="imperial"://英制计算
dataLst=function()
bmi=imperial(dataLst[0],dataLst[1])
return round(bmi,1)
elif choice=="0":
print("see you next time!")
os._exit(0)//退出程序
else:
print("invalid input!")//无效输入
bmi=BMI_1()
print("your bmi is:",bmi)//打印bmi值
time.sleep(0.5)
//以下内容为判断用户bmi
if bmi<20 and bmi>0:
print("you are underweight")
elif bmi>=20 and bmi<=25:
print("you are normal")
elif bmi>25 and bmi<=30:
print("you are overweight")
elif bmi>30:
print("you are obese")
else:
print("invalid bmi input!")
注:mac电脑注释python语言比较麻烦,这里就用//代替了
值得一提的是这里的assert函数。他的作用是:如果满足assert中的条件则程序继续运行,不满足则抛出AssertError。至于那个AssertError包括什么,我们是可以自己设定的。比如在这个程序中,AssertError抛出时程序会提示:no alphabet or negative number!。
package ELEM;
import java.util.Scanner;
//功能:检测用户的身体质量指数.
public class BMI {
private int cnt=0;
private int bmi;
private float weight;
private float height;
public static int bmiMetric(float weight,float height) {
float f=weight/(height*height);
int result=Math.round(f);//约成整数
return result;
}
public void deter_bmi(int bmi) {//判断bmi
if(bmi<20)
System.out.println("you are underweight");
else if(bmi>=20 && bmi<25)
System.out.println("you are normal");
else if(bmi>=25 && bmi<30)
System.out.println("you are overweight");
else
System.out.println("you are obese");
}
public void BMI_1() {
BMI obj=new BMI();
Scanner input=new Scanner(System.in);
System.out.println("enter your firstname:");
String firstname=input.next();
System.out.println("hello "+firstname);
while(cnt==0) {
System.out.println("do you want to choose metric or imperial:");
switch(input.next()) {
case "metric":
System.out.println("please enter your weight in kilogram and height in cms:");
weight=input.nextFloat();
height=input.nextFloat();
bmi=bmiMetric(weight,(float) (height/100.0));
cnt++;
break;
case "imperial":
System.out.println("please enter your weight in pounds and height in feet:");
weight=input.nextInt();
height=input.nextInt();
bmi=bmiMetric(Math.round(weight*0.4536),Math.round(height*0.3048));
cnt++;
break;
default:
System.out.println("invalid input!");
break;
}
}
System.out.println("thank you\n\n"+"your bmi is: "+bmi);
obj.deter_bmi(bmi);
}
public static void main(String[] args) {
BMI obj=new BMI();
obj.BMI_1();
}
}
#include
#include
#include
#include
void deter_bmi(int bmi){
if(bmi<20){
printf("you are underweight");
}else if(bmi>=20 && bmi<25) {
printf("you are normal");
}else if(bmi>=25 && bmi<30) {
printf("you are overweight");
}else{
printf("you are obese");
}
}
int bmi_calc(float weight, float height){
float result=weight/(height*height);
if((int)(result*10.0)%10>=5){
return (int)result+1;
}else{
return (int)result;
}
}
void BMI_main(){
char name[10], choice[8],choice_emp[8];
int bmi;
float weight, height;
printf("enter your name:");
scanf("%s",&name);
printf("hello %s, welcome to health care",name);
while(true){
printf("\ndo you want to choose metric or imperial(press 0 to exit):");
scanf("%s",&choice);
if(strcmp(choice,"metric")==0){
printf("please enter your weight in kg:");
scanf("%f",&weight);
printf("please enter your height in cm:");
scanf("%f",&height);
bmi=bmi_calc(weight,height/100.0);
break;
}
else if(strcmp(choice,"imperial")==0){
printf("please enter your weight in pounds:");
scanf("%f",&weight);
printf("please enter your height in inches:");
scanf("%f",&height);
bmi=bmi_calc(weight*0.4536,height*0.0254);
break;
}
else if(strcmp(choice,"0")==0){
printf("thank you for using, see you next time!");
exit(0);
}
else{
printf("invalid input!");
memcpy(choice,choice_emp,sizeof(choice));
}
}printf("thank you\n\n your bmi is:%d\n",bmi);
deter_bmi(bmi);
}
int main(){
BMI_main();
return 0;
}