Number guessing function using while and if else functions
显示 更早的评论
while x>a || x<a
%This classifies the user's input if the guess is above or below the program will tell the user their guess is incorrect
if x>a || x<a
%This classifies the user's input if the guess is above or below the program will tell the user their guess is incorrect
disp('You are to high/low please guess again \n')
else x=a
disp('You''ve got it!')
end
I'm wanting to run a program that uses a random number and outputs if the user defined input is a correct guess or else the program prompts the user to keep inputting guesses until they are correct.
5 个评论
Walter Roberson
2019-3-23
while x>a || x<a
is the same as
while ~isnan(x) && x ~= a
Anyhow, you did not ask us a question.
Our question would probably be "Where are you accepting input from the user?"
Mitchell
2019-3-23
Walter Roberson
2019-3-23
while x ~= a
if x > a
too high
else
too low
end
input a new guess
end
just right
Mitchell
2019-3-23
Walter Roberson
2019-3-23
else x > a
means the same thing as
else
disp(x > a)
Both branches of your code talk about the person being too high, with no branch saying they are too low.
回答(0 个)
类别
在 帮助中心 和 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!