Random numbers between 1 and 10 with limited attempts?
9 次查看(过去 30 天)
显示 更早的评论
Hello
I haven't found anything by searching this site, on the other hand I am new to this. I really would appreciate it if you could help me out on this one: I try to make a program which selects a random number between 1 and 10. One gets a limited amount of attempts to guess the right number. After 5 attempts, the programm should stop and tell you that you didn't succeed. I wrote some code and it does everything except stopping when one guessed the right number before attempt 5.
I tried changing everything but I only ended up in an infinite loop, after two hours I really would like to get help. :D
This is what I wrote:
Pickme=round(10*rand(1)); %Pick random number between 1 and 10
Guessedcorrectly=0;
while Guessedcorrectly == 0 %As long as the correct number has not been picked
for i=1:5
A(i)=input('Enter a number between 1 and 10:');% ask for new input
if A(i) == Pickme %Check whether number is correct
Guessedcorrectly = 1; %if so, have while-loop stop
disp('Correct! You have picked the right number!') %print message on screen
else disp('Wrong! Try again.') %if not, print on screen
end
end
end
The preview tells me that after for i=1:5, all this is put in a code/quote. I don't know why it does that. Can someone help me with my problem, i.e. why doesn't the script stop after one guessed the right number?
Greetings,
Keeks
0 个评论
采纳的回答
Matthew Eicholtz
2016-2-16
编辑:Matthew Eicholtz
2016-2-16
The script doesn't stop because it wants to finish the for-loop first. It will not check the line 'while Guessedcorrectly == 0' until the for-loop is done. There are many ways to solve this issue, but perhaps the easiest is to amend your code as follows:
...
if A(i) == Pickme %Check whether number is correct
Guessedcorrectly = 1; %if so, have while-loop stop
disp('Correct! You have picked the right number!') %print message on screen
break; % this tells MATLAB to leave the for-loop
...
2 个评论
Matthew Eicholtz
2016-2-17
Glad I could help, Keeks. I've been using MATLAB for 10+ years now, but I still learn new commands and tricks quite often. If you want to improve your MATLAB knowledge, keep using help and doc, search the documentation/forums online, test your skills with Cody, and keep practicing! You'll be fluent in MATLAB before you know it.
1. For-loops are not inherently bad, but I try to avoid them when possible (especially if computations can be vectorized). Nested for-loops can be a time sink.
2. The '%d' is a formatting operator that tells the fprintf function to look for a signed integer (in the first case, cnt) and insert that in the string. Check out the documentation for more info.
doc fprintf
3. Good thinking on using break in the while-loop of my alternate code, but it is not necessary in this case because my loop checks if the user guessed correctly before asking them to provide another guess.
更多回答(1 个)
Matthew Eicholtz
2016-2-16
编辑:Matthew Eicholtz
2016-2-16
Here is one alternative approach that removes the for-loop altogether:
pickme = randi(10); %the number we want the user to guess
cnt = 1;
maxcnt = 5; %max number of guesses
iscorrect = false;
while ~iscorrect && cnt<=maxcnt
x = input('Pick a number between 1 and 10: ');
if isequal(x,pickme)
fprintf('Correct! And it only took you %d tries!\n',cnt);
else
fprintf('Wrong! Try again. You have %d guesses left.\n',maxcnt-cnt);
cnt = cnt+1;
end
end
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!