Random number game repetition help
3 views (last 30 days)
Show older comments
Tyler Temme
on 16 Oct 2018
Reopened: Walter Roberson
on 22 Dec 2018
I am trying to create a number guessing game that keeps repeating until you guess the number. If I guess the number correct then I want it to ask to keep playing. If the user wants to keep playing, the code should repeat, but it is not working right now.
clear
clc
startChoice = input('Would you like to play this high-tech game? Enter 1 for yes and 0 for no!');
guessNumber = randi([1 50],1,1);
while startChoice == 1
guess = input('What number do you pick? ')
if guess < guessNumber
startChoice = input('You guessed to low. Would you like to keep trying? 1 for yes, 0 for no. ')
elseif guess > guessNumber
startChoice = input('You guessed to high. Would you like to keep trying? 1 for yes, 0 for no. ')
elseif guess == guessNumber
startChoice = input('You guessed correct! Would you like to play again? 1 for yes, 0 for no. ')
while startChoice == 1
guessNumber = randi([1 50], 1, 1);
guess = input('What number do you pick? ')
end
end
end
if startChoice == 0
disp('Wowwwww, you are no fun!')
end
0 Comments
Accepted Answer
Image Analyst
on 17 Oct 2018
Try this:
clc
playAgain = input('Would you like to play this high-tech game? Enter 1 for yes and 0 for no : ');
if playAgain ~= 1
helpdlg('Wowwwww, you are no fun!')
return;
end
highestNumber = 5;
computersNumber = randi([1 highestNumber],1,1);
usersGuess = input('What number do you pick? ');
while playAgain == 1 && usersGuess ~= 0
if usersGuess < computersNumber
usersGuess = input('You guessed too low. Enter your next guess, or 0 to quit : ');
elseif usersGuess > computersNumber
usersGuess = input('You guessed too high. Enter your next guess, or 0 to quit : ');
elseif usersGuess == computersNumber
playAgain = input('You guessed correct! Would you like to play again? 1 for yes, 0 for no. ');
if playAgain == 0
break;
end
% Get a new computer's choice.
computersNumber = randi([1 highestNumber], 1, 1);
usersGuess = input('What number do you pick? ');
end
end
if playAgain == 0
helpdlg('Wowwwww, you are no fun!')
end
0 Comments
More Answers (1)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!