Help switch case problem
16 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentario
Jan
el 13 de Feb. de 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?
Respuestas (2)
Azzi Abdelmalek
el 13 de Feb. de 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 comentarios
James
el 14 de Feb. de 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
el 14 de Feb. de 2013
Editada: Azzi Abdelmalek
el 14 de Feb. de 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.
Ver también
Categorías
Más información sobre Automated Driving Toolbox en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!