The while loop wont stop at the end when all attempts are failed.

2 次查看(过去 30 天)
riddles=true; %This will help end or continue the whole program
while(riddles==true)
riddlecounter1=1;
riddlescheck=true; %This will help end or continue the code
while(riddlescheck==true && riddlecounter1<4)
riddlecounter1=riddlecounter1+1;
%Asking the user 3 riddles.
riddle1prompt='The person who built it sold it. The person who bought it never used it. The person who used it never saw it. What is it? ';
riddle1=input(riddle1prompt,'s');
fprintf('\n')
if strcmpi(riddle1,'Coffin')
%Will tell the user if they answered correctly and who died.
fprintf('Nice job, sadly Jack and Ethan did not fair so well. Oh well.')
fprintf('\n')
fprintf('Here is your second riddle!')
fprintf('\n')
riddlescheck=false;
else
%The user can try to get the riddle 3 tries then the program ends.
if riddlecounter1==2
fprintf('Oh no, what did I say about not messing up!')
fprintf('\n')
fprintf('Dont mess this up again!')
fprintf('\n')
end
if riddlecounter1==3
fprintf('\n')
fprintf('Haha, you only have 1 more try. Be Careful.')
fprintf('\n')
fprintf('Your hint for this riddle is; its present at a funeral.')
fprintf('\n')
end
if riddlecounter1==4
fprintf('Oh no, what did I say. Now youve already failed my game.')
fprintf('\n')
fprintf('Its GAME OVER for you!!')
riddlescheck=false; % <-----
riddles=false; %<----
end
end %End to the if statement
end %End to riddlescheck
riddlecounter2=1;
riddlecheck2=true;
while (riddlecheck2==true && riddlecounter2<4)
riddlecounter2=riddlecounter2+1;
riddle2prompt='I dont have eyes, but once I did see. Once I had thoughts, but now Im white and empty. What am I? ';
riddle2=input(riddle2prompt,'s');
if strcmpi(riddle2,'Skull')
%Asking the final riddle because the user answered the other two right.
fprintf('\n')
fprintf('*sighs* You arent supposed to get these. Your final riddle is;')
fprintf('\n')
riddlecheck2=false;
else
%The game ends if answer is wrong.
if riddlecounter2==2
fprintf('What did I say about not messing up. Get it right!')
fprintf('\n')
fprintf('You have 2 more tries.')
fprintf('\n')
end
if riddlecounter2==3
fprintf('Haha, you only have 1 more tries. Careful.')
fprintf('\n')
fprintf('Your hint for this riddle is; your brain makes its home here.')
fprintf('\n')
end %End to if statement
if riddlecounter2==4
fprintf('Oh no, what did I say. Now youve already failed my game.')
fprintf('\n')
fprintf('Its GAME OVER for you!!')
riddlescheck2=false;
riddles=false; %<-------
end %End to if statement
end %End to an if statement
end %End to while loop
end %End to riddles
%Ive put arrows to where i thought the two variables would make the code end but it doesnt. I dont understand why it continues.

回答(1 个)

Voss
Voss 2020-11-24
I think, after the end of the first inner while loop, which starts like this:
while(riddlescheck==true && riddlecounter1<4)
you need this:
if ~riddles
break
end
This will break out of the outer while loop and end the game if the user has gotten the first riddle wrong three times. (Note that you don't need a similar check after the second inner while loop because the outer while loop takes care of it.)

类别

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