Repeated user input for even numbers

17 ビュー (過去 30 日間)
Hariyali Ramesh
Hariyali Ramesh 2019 年 1 月 24 日
コメント済み: Hariyali Ramesh 2019 年 1 月 24 日
The code needs to keep asking for user input until an even number is inputted. I've got this code so far:
a = input('Input a value: ');
b = 2;
if rem(a,b) == 0
disp('Your value is even.')
else
a = input('Input a value of a');
end
but this only works twice when an odd number has been entered. How do I get it to keep asking for an input until an even number has been entered?
  2 件のコメント
Sarah Crimi
Sarah Crimi 2019 年 1 月 24 日
%Create variable modvalue and use while loop to remain asking.
modvalue = 1;
while(modvalue~=0)
a = input('Input a value: ');
b = 2;
modvalue = rem(a,b)
end
disp('Your value is even.')
Hariyali Ramesh
Hariyali Ramesh 2019 年 1 月 24 日
Thanks, this helped.

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

採用された回答

Adam Danz
Adam Danz 2019 年 1 月 24 日
編集済み: Adam Danz 2019 年 1 月 24 日
The while loop will continually ask for a non-zero even integer until one is entered.
prompt= 'Input a value: '
z = 1;
while mod(z,2)~=0
z= input(prompt)
end
Very similar question was asked on the same day here:
  1 件のコメント
Hariyali Ramesh
Hariyali Ramesh 2019 年 1 月 24 日
Thanks for the help.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCell Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by