필터 지우기
필터 지우기

How to categorize a range

조회 수: 3 (최근 30일)
Takashi Fukushima
Takashi Fukushima 2019년 11월 1일
댓글: Takashi Fukushima 2019년 11월 2일
Hello,
I would like to make write a program like categorizing based on answers.
I put what I have now.
a=input("Enter your height in cm: ");
b=input("Enter your weight in g: ");
Gender=input("Enter number for your gender 1=male 2=female: ");
BMI=(b/1000)/(a/100)^2;
disp("Your BMI is: " + BMI);
Status=0;
switch Gender
case 1
if BMI<20
Status="Underweight";
if BMI>=20 & BMI<25
Status="Regular weight";
if BMI>=25 & BMI<30
Status="Overweight";
if BMI>=30 & BMI<40
Status="Adiposity";
if BMI>=40
Status="Severe Adiposity";
end
end
end
end
end
case 2
if BMI<19
Status="Underweight";
if BMI>=19 && BMI<24
Status="Regular weight";
if BMI>=24 && BMI<30
Status="Overweight";
if BMI>=30 && BMI<40
Status="Adiposity";
if BMI>=40
Status="Severe Adiposity";
end
end
end
end
end
otherwise
disp("invalid");
end
disp("Your health status: " + Status);
It does not have an error, but it applies only the first "if" which shows "Status=Underweight", and other "if" shows as "Status=0"
I really appreciate if you can solve this problem.
Thanks,

채택된 답변

David Hill
David Hill 2019년 11월 2일
function BMIdetermination()
a=input("Enter your height in cm: ");
b=input("Enter your weight in g: ");
Gender=input("Enter number for your gender 1=male 2=female: ");
BMI=(b/1000)/(a/100)^2;
disp("Your BMI is: " + BMI);
lookup={'Underweight','Regular weight','Overweight','Adiposity','Severe Adiposity'};
if Gender==1
Status=lookup{find(histcounts(BMI,[0,20,25,30,40,100]))};
else
Status=lookup{find(histcounts(BMI,[0,19,24,30,40,100]))};
end
disp("Your health status: " + Status);
end
  댓글 수: 1
Takashi Fukushima
Takashi Fukushima 2019년 11월 2일
Thank you so much! I am amazed that the answer can be this simple.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Gamma Functions에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by