Help switch case problem

15 次查看(过去 30 天)
THANH NGUYEN
THANH NGUYEN 2013-2-13
The requirement of this assignment is to calculate the Ideal Body Weight (IBW). The following equations are used to compute the IBW, where height is in inches and the IBW is in kilograms. The user will need to specify the gender by entering a character notation, like M/m or F/f.
What's wrong with my code? I can't run it.
gender=input('Enter the gender (F/f or M/m):');
ibwWeight=IBW*2.2046 % pounds
switch(gender)
case {'F', 'f'}
IBW = 45.5 + 2.3 * (Height-60);
case {'M', 'm'}
IBW = 50.0 + 2.3 * (Height-60);
end
fprintf('The IBW is: %.1f pounds\n',ibwWeight)
  1 个评论
Jan
Jan 2013-2-13
Please explain more details of "I can't run it". Do you get an error message, do the results differ from your expectations, do you have Matlab installed?

请先登录,再进行评论。

回答(2 个)

Azzi Abdelmalek
Azzi Abdelmalek 2013-2-13
gender=input('Enter the gender (F/f or M/m):','s');
Height=100
switch(gender)
case {'F', 'f'}
IBW = 45.5 + 2.3 * (Height-60);
case {'M', 'm'}
IBW = 50.0 + 2.3 * (Height-60);
end
ibwWeight=IBW*2.2046
fprintf('The IBW is: %.1f pounds\n',ibwWeight)
  4 个评论
James
James 2013-2-14
I get an error when I run my program like this ^^^ The error is
Error using input Undefined function or variable 'M'.
Error in Lab (line 84) gender = input( 'Enter the gender: ')
I've been fiddling with it for a while and have gotten nowhere
Azzi Abdelmalek
Azzi Abdelmalek 2013-2-14
编辑:Azzi Abdelmalek 2013-2-14
If you use
gender=input('Enter the gender (F/f or M/m):');
You are expecting a string 'f/m' or 'F/M' as an input. then when you enter for example F, there will be an error. To allow entering F you to add 's' as an option in:
gender=input('Enter the gender (F/f or M/m):','s');
I don't know if you've read my answer.

请先登录,再进行评论。


THANH NGUYEN
THANH NGUYEN 2013-2-13
编辑:THANH NGUYEN 2013-2-13
the target weight for target bmiw. I don't know why I got the wrong pound. It should be 184.3 pounds for bmi=25, but i got 165 pounds. The third can't run due to some error which I dont know
% Prompt the user to enter a value for weight in pounds and height in inches.
Weight=input('Enter the weight in pounds:');
Height=input('Enter the height in inches:');
%Compute conversion
kiloWeight=Weight/2.2046; %kg
meterHeight=Height*2.54/100; %meters
disp(' ')
%Display BMI classification
bmi=kiloWeight/meterHeight^2;
fprintf('The BMI is: %.2f\n',bmi)
if bmi<18.5
disp('BMI classification:underweight');
elseif 18.5 <=bmi & bmi < 24.9
disp('BMI classification:Normal weight');
elseif 25 <=bmi & BMI < 29.9
disp('BMI classification:Overweight');
else
disp('BMI classification: Obese');
end
disp(' ')
% Target BMI and Weight
bmiTarget=input('Enter the target BMI:');
% Compute target weight
kilotargetWeight=bmi*meterHeight^2; %kg
%Compute Conversion
poundtargetWeight=kilotargetWeight*2.2046;
fprintf('The target weight is: %.1f pounds\n',poundtargetWeight)
disp(' ')
gender=input('Enter the gender (F/f or M/m):','s');
switch(gender)
case {'F', 'f'}
IBW = 45.5 + 2.3 * (Height-60);
case {'M', 'm'}
IBW = 50.0 + 2.3 * (Height-60);
end
fprintf('The IBW is: %.1f pounds\n',ibwWeight)

类别

Help CenterFile Exchange 中查找有关 Automated Driving Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by