フィルターのクリア

How do I make a while loop that will return to a statement unless a condition is met

1 回表示 (過去 30 日間)
I am trying to make a while loop that will goes on forever unless a condition is met, in this case I want the loop to keep repeating unless I enter the word imperial or metric. And when I enter other words it will continue ask the question "Please make a choice", the code i have here just opts out whenever i enter other words
clear,clc
x = [];
while isempty(x)
x = input('Please make a choice (imperial or metric) - ','s');
if strcmp(x,'imperial')
disp('yes')
if strcmp(x,'metric')
disp('no')
end
end

採用された回答

Walter Roberson
Walter Roberson 2018 年 4 月 16 日
If strcmp()
...
elseif strcmp()
...
else
Give message
x = ''
end
  2 件のコメント
Quan Seah
Quan Seah 2018 年 4 月 16 日
I tried your answer it seems like i still get the same problem when i enter words other than imperial and metric, it just opts out of the loop
Walter Roberson
Walter Roberson 2018 年 4 月 16 日
Please show your revised code

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by