code is too long

a = 4;
b = input ('2+2=? ')
if b == a
'ok'
else
b = input ('retry ')
if b == a
'ok'
else
b = input ('retry ')
if b == a
'ok'
else 'NOK'
end
end
end

 采纳的回答

Image Analyst
Image Analyst 2013-3-31
编辑:Image Analyst 2013-3-31
I think you want
a = 4;
b = 0;
while b ~= a
b = input ('2+2=? ');
if b == a
break;
end
fprintf('Incorrect. Try again.\n');
end
fprintf('Correct.\n');

1 个评论

If you want to limit it to three tries, add a counter:
a = 4;
b = 0;
counter = 1;
while b ~= a && counter <= 3
b = input ('2+2=? ');
if b == a
break;
end
fprintf('Incorrect. Try again.\n');
counter = counter + 1;
end
if counter <= 3
fprintf('Correct.\n');
end

请先登录,再进行评论。

更多回答(1 个)

Nicolò
Nicolò 2013-3-31

0 个投票

Yes, but I would like to repeat this code up to three times if the number entered is incorrect

2 个评论

This is not an answer to your question. It should have been a comment to my answer so I'll put the response there.
thanks

请先登录,再进行评论。

类别

在 帮助中心 和 File 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