フィルターのクリア

While loop and if statement to choose between 2 criteria?

2 ビュー (過去 30 日間)
Matilde Garcia
Matilde Garcia 2015 年 12 月 3 日
コメント済み: Matilde Garcia 2015 年 12 月 3 日
I am looking to write code which gives me x=0 if n=1 and x=7 if n=2 (n being user generated). However I want to continue prompting the user to input n =1 or n=2 if the value the user inputed was different than 1 or 2.

採用された回答

Kirby Fears
Kirby Fears 2015 年 12 月 3 日
編集済み: Kirby Fears 2015 年 12 月 3 日
Start off with an n value that allows you to enter the while loop. The while loop should continue as long as n is not 1 or 2. After the while loop is over, you know n is either 1 or 2, so just test the n value and assign x accordingly.
n = 0;
while sum(n==[1 2])<1,
n = input('Please input value for n:\n');
end
if n==1
x = 0;
else
x = 7;
end
  1 件のコメント
Matilde Garcia
Matilde Garcia 2015 年 12 月 3 日
Thanks a lot. It worked perfectly.

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

その他の回答 (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