How can I ask the user to input units of either m or in, then have the program keep asking them for units until they input either m or in?

1 次查看(过去 30 天)
Asks the user the unit of l. The program must accept the following units or keep asking for a unit: a. m b. in
  1 个评论
bidyut mandi
bidyut mandi 2018-4-13
编辑:bidyut mandi 2018-4-13
Ask the user the unit of I means disp('Enter the unit of I').I don't get that the program everytime ask the unit of a.m,b.in or single time.Please clarify this

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2018-4-14
I suggest you use
iChoice = menu('Select a unit', 'inches', 'metres');
This will return 1 for inches and 2 for metres.
  1 个评论
Tanner Larkey
Tanner Larkey 2018-4-14
Thanks! Using the menu makes things a lot easier! Here is what I came up with:
units = menu('Select a unit','inches','meters');
if units == 1
g = 384;
fprintf('Gravity is 384 in/s^2')
else
g = 9.81;
fprintf('Gravity is 9.81 m/s^2')
end

请先登录,再进行评论。

更多回答(1 个)

ES
ES 2018-4-13
disp('Choose the Unit.');
disp('[0] Inches.');
disp('[1] Metres.');
iChoice=input('Please make a choice. ');
while(~isnumeric(iChoice) ||(uint8(iChoice)>1))
iChoice=input('Please make a valid choice. Enter 0 to quit. ');
end
  2 个评论
Tanner Larkey
Tanner Larkey 2018-4-14
Then how would I write a script that correctly determines the value of gravitational acceleration g, consistent with the units they input and print it on screen? (if they input meters gravity would be 9.81 m/s^2, if they input inches gravity would be 384 in/s^2)

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by