How to create a question that loops until it receives correct answer?

2 次查看(过去 30 天)
How do I create a program that repeatedly asks the question until the user gives the correct answer? Below is a simple code to explain what exactly I'm asking...
prompt = 'What age is Seán? ';
age = (input(prompt));
ageans = 21;
if age == ageans
disp('Correct!');
else
disp('Wrong');
end
So if someone were to answer 37, rather than 21, how would I change the code so that it would loop back to the original question to ask the user again?

采纳的回答

Adam
Adam 2017-4-24
age = 0;
ageans = 21;
while age ~= ageans
prompta = 'What age is Seán? ';
age = (input(prompta));
if age == ageans
disp('Correct!');
else
disp('Wrong');
end
end

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by