Continue a loop when entered an option
2 views (last 30 days)
Show older comments
In my code I want to begin the loop again when I enter 1 and quit the program when entered 0.
clc
clear
syms result guess
corrects = 0;
n = 10;
for k = 1:n
x = randi([3 10],1,1);
y = randi([3 10],1,1);
result = x * y;
guess = input(sprintf('What is %d * %d?: ', x,y));
if (result == guess)
disp('Correct!')
corrects = corrects + 1;
else
fprintf('Wrong! The correct answer is %d\n', result);
end
end
fprintf('You were right %d out of 10!\n', corrects);
option = input('Do you want to do a new round (1 = yes, 0 = no): ');
if option == '1'
%start the loop again otherwise quit
end
Answers (1)
Guillaume
on 10 Apr 2019
Just wrap your code in a while loop:
option = 1;
while option == 1
%... your code here
option = input('Do you want to do a new round (1 = yes, 0 = no): ');
end
Any reason you're using the symbolic toolbox? As far as I can tell it's completely unneeded.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!