In a set up that provides guesses at a random number: how to get Matlab to display the guess that was correct (e.g. 4th)?

1 次查看(过去 30 天)
I've set up a loop including while, if, and else functions so that five guesses are made before a result is given whether these include the correct or incorrect answer but, in the event of correct, how to get it to display which guess was correct?
  1 个评论
Becky CNS
Becky CNS 2018-2-25
This is the syntax so far : How to change script so it displays correct if answer was included in the 5 guesses but also which guess gave the correct answer?
pickme1_100 = round(100.*rand(1));
Counter = 0;
while Guessedcorrectly ==0 A = input ('enter number between 1 and 100');
Counter = Counter + 1;
if A == pickme1_100 Guessedcorrectly = 1 && Counter == 5;
disp('Correct!');
elseif Guessedcorrectly ==0&& Counter >= 5
disp ('All are wrong, try again!')
end
while Guessedcorrectly ==0
A = input ('enter number between 1 and 100');
Counter = Counter + 1;
if A == pickme1_100
Guessedcorrectly = 1 && Counter == 5;
disp('Correct!');
elseif Guessedcorrectly ==0&& Counter >= 5
disp ('All are wrong, try again!')
end
end
end

请先登录,再进行评论。

采纳的回答

Image Analyst
Image Analyst 2018-2-25
If you want to guess sequentially, you could have the guess be the index of a for loop.
If you want to guess randomly, you could use a while loop and get the guess by using the randi() function.
Note: guessing will only work if the guesses and correct answer are all integers, otherwise there is virtually no chance you would guess it for a floating point number.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Sources 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by