Why do I keep on getting wrong choice?

So just a little background info, I am trying to write a script that uses a menu and a switch so the user can choose to compute and report one of the following three pieces of information: The maximum height, the minimum velocity, or the time to hit. The equations I use for maximum height, minimum velocity, and time to hit are including in my code and they are for a projectile (such as a thrown ball) launched with an initial speed of V0 at an angle A to the horizontal (use input functions). My given is 9.80 m/s2 for the acceleration of gravity, g. I want to test my script for an initial velocity of 40 m/s and an angle of 30°. My code so far is this:
v0 = input('Enter the initial speed: ');%meters per second
A = input('Enter the angle: ');%degrees
g = 9.8;%meters per second squared
quantity = menu('Choose a quantity','height','velocity','time to hit');
A_rad = A*pi/180;
switch quantity
case 'height'
height = (v0^2*sin(A_rad))/(2*g);
case 'velocity'
velocity = v0*cos(A_rad);
case 'time to hit'
time_to_hit = 2*(v0/g)*sin(A_rad);
otherwise
disp('Wrong choice')
end
The problem is when I run my script, my menu pops up with the three options. But when I select any of the options(either height, velocity, or time to hit) I get this:
>> ThrownBall
Enter the initial speed: 40
Enter the angle: 30
Wrong choice
My menu is correct but somehow I keep getting 'Wrong choice', any pointers on how to fix my code so it can run correctly??

1 comentario

A tip to make the code a bit more robust: use the 's' option, and wrap input in str2double:
v0 = str2double(input('Enter the initial speed: ','s'));
This will correctly return any scalar numeric value, but if the user enters an invalid value (e.g. multiple values, or something that is not a number) it will simply return NaN.

Iniciar sesión para comentar.

 Respuesta aceptada

Birdman
Birdman el 28 de Mzo. de 2018
Editada: Birdman el 28 de Mzo. de 2018
quantity returns either 1, 2 or 3. Therefore your code should look like this:
v0 = input('Enter the initial speed: ');%meters per second
A = input('Enter the angle: ');%degrees
g = 9.8;%meters per second squared
quantity = menu('Choose a quantity','height','velocity','time to hit');
A_rad = A*pi/180;
switch quantity
case 1
height = (v0^2*sin(A_rad))/(2*g);
case 2
velocity = v0*cos(A_rad);
case 3
time_to_hit = 2*(v0/g)*sin(A_rad);
otherwise
disp('Wrong choice')
end
1 for height, 2 for velocity and 3 for time to hit.

3 comentarios

T.Y. Hilton
T.Y. Hilton el 28 de Mzo. de 2018
Editada: T.Y. Hilton el 28 de Mzo. de 2018
Gotcha I see what you are saying! Also I'm sorry if I'm being a pain in the butt, but when I changed it my code ran like this,
>> ThrownBall
Enter the initial speed: 40
Enter the angle: 30
It ran without giving me the answer ! I'm new to Matlab so I'm not quite sure how to fix this..maybe there is something wrong with my switch function?
That is because every statement in case has a semicolon at the end, telling it to not display the answer. Do this:
v0 = input('Enter the initial speed: ');%meters per second
A = input('Enter the angle: ');%degrees
g = 9.8;%meters per second squared
quantity = menu('Choose a quantity','height','velocity','time to hit');
A_rad = A*pi/180;
switch quantity
case 1
height = (v0^2*sin(A_rad))/(2*g);
disp(height)
case 2
velocity = v0*cos(A_rad);
disp(velocity)
case 3
time_to_hit = 2*(v0/g)*sin(A_rad);
disp(time_to_hit)
otherwise
disp('Wrong choice')
end
T.Y. Hilton
T.Y. Hilton el 28 de Mzo. de 2018
It worked! Thank you!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Programming en Centro de ayuda y File Exchange.

Preguntada:

el 28 de Mzo. de 2018

Comentada:

el 28 de Mzo. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by