フィルターのクリア

I want to determine whether the user wishes to work with their angles in degrees or radians

1 回表示 (過去 30 日間)
Jimmy
Jimmy 2012 年 10 月 30 日
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

回答 (2 件)

Walter Roberson
Walter Roberson 2012 年 10 月 30 日
input() expects numeric input unless you use the 's' option.
If you want to compare strings, use strcmp() or strcmpi()

dimitris
dimitris 2012 年 10 月 30 日
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.

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by