MATLAB Answers

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?

조회 수: 3(최근 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 13 Apr 2018
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 14 Apr 2018
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 14 Apr 2018
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 13 Apr 2018
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

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by