- #1
_N3WTON_
- 351
- 3
Homework Statement
Write a program for a simple guessing game. The program should first prompt the user for a
number between 1 and 100. After each guess the program should inform the user whether the guess was too high or too low. When the user picks the correct number, inform them and let them know how many guesses it took. Also, after the correct number has been picked, ask them if they would like to play again. If they enter 'N' the program should terminate. If they enter 'Y' the program should run again, prompting the user to guess a new random number.
Homework Equations
None
The Attempt at a Solution
I have had no problem doing the first part of the program, this is my code thus far:
clc
TargetNum = randi(100);
UserGuess = 0;
GuessNumber = 0;
while ((UserGuess ~= TargetNum))
GuessNumber = GuessNumber + 1;
UserGuess = input('Guess an integer between 1 and 100 \n');
if (UserGuess < TargetNum)
fprintf('The number "%i" is too low \n', UserGuess)
elseif (UserGuess > TargetNum)
fprintf('The number "%i" is too high \n', UserGuess)
else
fprintf('You guessed the correct number: %i \n',TargetNum)
fprintf('This required "%i" guesses \n', GuessNumber)
end
end
However, I am having some trouble figuring out how to get the program to run again if the user decides to play again. I have experience using Java, so I am fairly knowledgeable when it comes to programming. However, in JAVA I would probably use a "Boolean" operator to get the program to run again, but I cannot do this in MatLab. I also decided to look up how to compare strings in MatLab using the "TF = strcmpi(string,string)" technique. However, I am unsure if this would work, or even how to go about doing it. Any advice on how to complete this program would be greatly appreciated. Thanks.