How to create multiple conditions for invalid inputs?

1 回表示 (過去 30 日間)
RealA
RealA 2019 年 4 月 24 日
コメント済み: Rena Berman 2019 年 5 月 15 日
Hey everyone, I'm currently building a unit converter program for an assignment and I'm struggling to create a loop that repeats a certain message if certain conditions are not met. For example if I were to leave x blank and spam enter it would repeat the message which is want I want. However if I were to enter a value that is below 0 or above 10 , and then leave x blank and press enter, I would get an error(red line), in this instance I would like my code to reapeat the isempty(x) loop. How would I achieve this?
Thanks in advance
x=input(' enter a number from the selection above: ');
while isempty(x)
disp('You are required to enter a number from the options above!')
x=input(' enter a number from the selection above: ');
end
while(x<=0 || x>=10)
disp('The number must be between 1 and 9, you have enterd a value oustide the limit')
x=input(' enter a number from the selection above: ');
end
  1 件のコメント
Rena Berman
Rena Berman 2019 年 5 月 15 日
(Answers Dev) Restored edit

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

採用された回答

Walter Roberson
Walter Roberson 2019 年 4 月 24 日
while ~isscalar(x) || x <= 0 || x >= 10
  2 件のコメント
RealA
RealA 2019 年 4 月 24 日
Thanks for the response, just one thing though if I were enter a value that is below 0 or above 10 and leave the input blank and press enter, I would like my code to also run the same display message as the isempty loop. How could I do that?
Walter Roberson
Walter Roberson 2019 年 4 月 24 日
prompt = ' enter a number from the options above: ';
x = input(prompt);
while ~isscalar(x) || x <= 0 || x >= 10
disp('You are required to enter a number from the options above!');
x = input(prompt);
end

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeFunctional Programming についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by