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일

0 개 추천

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

Image Analyst
Image Analyst 2013년 3월 31일
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

Image Analyst
Image Analyst 2013년 3월 31일
This is not an answer to your question. It should have been a comment to my answer so I'll put the response there.
Nicolò
Nicolò 2013년 3월 31일
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