Nested While loop functions
9 次查看(过去 30 天)
显示 更早的评论
Given: I have created a simple number guessing game that prompts the user to guess a random number between 1 & 100 and will inform them if their guess is too high or too low, then tell them when they have guessed right.
Find: I need to find a way to use another while loop, (either inside of or side by side), to ask the user if they would like to play again after they have guessed the right answer.
Issue: I just don't even know where to begin. I got the first loop running smooth, but I am unsure of how to use another loop to loop that loop if prompted to do so by the user.
My Solution: I didn't suppress the output of num so I can troubleshoot the code.
num=randi([1,100])
guess=input('Welcome to the game! Guess a number between 1 & 100: ');
count=1;
while num~=guess
count=count+1;
if guess>100 || guess<1
disp('Invalid Entry')
elseif guess>num
disp('Your guess is too high')
else
disp('Your guess is too low')
end
guess=input('Enter a new number: ');
end
disp('You guessed right! What a surprise!')
fprintf('You guessed this many times: %i\n',count)
if count==1
disp('You cheated!')
elseif count<4
disp('Not bad.')
else
disp('Practice makes perfect...')
end
% My thinking is to somehow put my working "game" into another while loop
% and ask the user to input a 1 if they want to play again, or a 2 if not, but how would I do
% that? while r,(response)=1,(yes)? This would be my first nested loop.
12 个评论
Voss
2024-4-16
That's what indentation is for. Each "end" lines up with its corresponding while/for/if.
采纳的回答
Shubham
2024-4-8
Hi Kyle,
To achieve the functionality of asking the user if they want to play the game again after they've guessed the number correctly, you can indeed use another while loop around your existing game loop. This outer loop will control whether the game restarts based on the user's input. Here's how you can structure it:
- Initialize a variable before the outer loop to control whether the game should continue. Let's call this variable playAgain and set it to true initially.
- Wrap your existing game code inside this outer while loop, which continues as long as playAgain is true.
- After the inner game loop (where the user has guessed the number correctly), prompt the user to ask if they want to play again.
- Based on the user's input, update the playAgain variable. If the user chooses to play again, the outer loop will continue; otherwise, it will exit, ending the game.
Here's how your code could look with these modifications:
playAgain = true; % Initialize the play again control variable
while playAgain
num = randi([1,100]); % Generate a random number
fprintf('Welcome to the game! Guess a number between 1 & 100: ');
guess = input('');
count = 1;
while num ~= guess
count = count + 1;
if guess > 100 || guess < 1
disp('Invalid Entry');
elseif guess > num
disp('Your guess is too high');
else
disp('Your guess is too low');
end
guess = input('Enter a new number: ');
end
disp('You guessed right! What a surprise!');
fprintf('You guessed this many times: %i\n', count);
if count == 1
disp('You cheated!');
elseif count < 4
disp('Not bad.');
else
disp('Practice makes perfect...');
end
% Ask the user if they want to play again
playAgainResponse = input('Do you want to play again? (yes=1/no=0): ');
if playAgainResponse == 1
playAgain = true; % User wants to play again
else
playAgain = false; % User does not want to play again, exit the loop
end
end
disp('Thanks for playing!');
In this code, after the user successfully guesses the right number, they are prompted to decide whether they want to play again. If the user inputs 1, the game restarts. If they input 0, the message 'Thanks for playing!' is displayed, and the program exits the outer loop, effectively ending the game. This approach allows for the game to be played multiple times without restarting the script manually.
I hope this helps!
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!