Why do I keep on getting wrong choice?
Mostrar comentarios más antiguos
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
Stephen23
el 28 de Mzo. de 2018
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.
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Programming en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!