Help switch case problem

16 ビュー (過去 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 Center および File ExchangeAutomated Driving Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by