code is too long

1 view (last 30 days)
Nicolò
Nicolò on 31 Mar 2013
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

Accepted Answer

Image Analyst
Image Analyst on 31 Mar 2013
Edited: Image Analyst on 31 Mar 2013
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 Comment
Image Analyst
Image Analyst on 31 Mar 2013
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

Sign in to comment.

More Answers (1)

Nicolò
Nicolò on 31 Mar 2013
Yes, but I would like to repeat this code up to three times if the number entered is incorrect
  2 Comments
Image Analyst
Image Analyst on 31 Mar 2013
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ò on 31 Mar 2013
thanks

Sign in to comment.

Categories

Find more on Loops and Conditional Statements 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!