how to ask a for input, and keep asking the same question until the right input is inputted?
13 views (last 30 days)
Show older comments
Man Hon Morrison Kong
on 17 Jan 2019
Edited: per isakson
on 20 Jan 2019
If I want the user to input an even number, and keep asking the user to input another number if they input an odd number or zero.
3 Comments
Accepted Answer
per isakson
on 20 Jan 2019
Edited: per isakson
on 20 Jan 2019
One way. n avoids running forever.
%%
n = 0;
while n <= 12
n = n + 1;
x = input('Please, input an even number ');
if x==0
disp('This number is zero, please rerun the programme again, sorry this is not automatic')
elseif mod(x,2) == 0
disp('the number is even')
break
else
disp('This is an odd number, please rerun the programme again, sorry this is not automatic')
end
end
0 Comments
More Answers (0)
See Also
Categories
Find more on Function Creation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!