Guessing game help (beginner programmer)
显示 更早的评论
I need to create a guessing game program using while loops where the user needs to guess between 1 and 10 and they only get three tries. every time they get it wrong a message will display that their guess is wrong and to re enter another number. if its correct they will get a "right!" display but if they guess correct and tries exceeded 3 times a message will display that they got the right number but they exceeded tries and lost the game.
this is the program i have created so far and im stuck on the little things like how do i generate the program so that it allows 3 tries before you lose. this program might be a bit mixed up.
secretnumber = randi([1 10]);
numguesses = 0;
disp('I am thinking of a number from 0 to 10.');
disp('You must guess what it is in three tries.');
guess = input('Enter a guess: ');
while guess ~= secretnumber
numguesses = numguesses + 1;
if numguesses == secretnumber
disp('Right!');
fprintf('it took you %g guesses. You have won the game!');
end
endwhile
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Just for fun 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!