Help switch case problem
16 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentar
Jan
am 13 Feb. 2013
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?
Antworten (2)
Azzi Abdelmalek
am 13 Feb. 2013
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 Kommentare
James
am 14 Feb. 2013
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
am 14 Feb. 2013
Bearbeitet: Azzi Abdelmalek
am 14 Feb. 2013
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.
Siehe auch
Kategorien
Mehr zu Automated Driving Toolbox finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!