I want to determine whether the user wishes to work with their angles in degrees or radians
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hi, I have written the following code to determine whether the user want their angle to be calculated in degrees or in radians
type= input('Enter D to work angle in degrees or R for radians')
V= input('Enter velocity ');
a= input('Enter angle ');
cosine;
sine;
if (type==r | R)
cosine=cos;
sine=sin;
elseif (type==d | D)
cosine=cosd;
sine=sind;
else
output('You failed to input a correct answer');
end
It won't work for me. I would also liek to allow the user to be able to reinput their choice in the last else statement in case they hit an invalid key.
Thanks Chris
1 comentario
Walter Roberson
el 30 de Oct. de 2012
Please use better tags for this question; see http://www.mathworks.co.uk/matlabcentral/answers/43073-a-guide-to-tags
Respuestas (2)
Walter Roberson
el 30 de Oct. de 2012
input() expects numeric input unless you use the 's' option.
If you want to compare strings, use strcmp() or strcmpi()
0 comentarios
dimitris
el 30 de Oct. de 2012
type= input('Enter D to work angle in degrees or R for radians')
V= input('Enter velocity ');
a= input('Enter angle ');
cosine;
sine;
if (type==r | R)
cosine=cos(a);
sine=sin(a);
elseif (type==d | D)
cosine=cosd(a);
sine=sind(a);
else
type=input('You failed to input a correct answer, press r for rads or d for degrees');
if (type==r | R)
cosine=cos(a);
sine=sin(a);
elseif (type==d | D)
cosine=cosd(a);
sine=sind(a);
else
end
Or you can use a while loop if you want the user to reinput his choice until it's correct.
0 comentarios
Ver también
Categorías
Más información sobre Loops and Conditional Statements 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!