while loop ends before the question is answered?

r=randi(51)+49;
%the random integer generator will give a number inbetween 50 nad 100 for the user to guess.
disp('I''m thinking of a number between 50 and 100. Can you guess what it is?')
disp('I''m going to have you guess the number and I''ll let you know if it''s right.')
disp(' ')
disp('What is you''re guess')
user_number=input('Please input your number here: ');
clc
%laying the ground rules for the game, so it is clear and easy to understand.
disp('Here''s your guess:')
disp(user_number)
%Making the answer placement more appearent.
count=0;
numguess=0;
chance=0;
while r>=50 || r<=100
r=r;
if user_number==r
disp('Nice job you guessed the right number!')
elseif user_number>r;
disp('You guess is too high. try again.')
user_number=input('enter new guess here');
elseif user_number<r;
disp('Your guessed the wrong number. try again.')
user_number=input('enter new guess here');
end
break
end
%I'm strugling to find a way to continues the loop when the question is asked wrong.

 採用された回答

James Tursa
James Tursa 2020 年 4 月 7 日
編集済み: James Tursa 2020 年 4 月 7 日

1 投票

Your break is in the wrong place. It should be under one of the if conditions:
if user_number==r
disp('Nice job you guessed the right number!')
break
And you might give the user better feedback if the guess is too low:
elseif user_number<r;
disp('Your guess is too low. try again.')

7 件のコメント

Todd Wyzkiewicz
Todd Wyzkiewicz 2020 年 4 月 7 日
When I move the (break) it gives me an endless matrix of the display read out, which was the first problem I ran into. The break was sort of a bandaid, but really thanks for pionting out the second thing.
James Tursa
James Tursa 2020 年 4 月 7 日
編集済み: James Tursa 2020 年 4 月 7 日
That's because your while loop condition is wrong. This
while r>=50 || r<=100
should be this
while true
Todd Wyzkiewicz
Todd Wyzkiewicz 2020 年 4 月 7 日
I see where youre coming from, but the problem that Im facing is this contiues matrix that I get in the command window.
this is what I see when I answer the question once.
Your guess is too low of a number. try again.
Your guess is too low of a number. try again.
Your guess is too low of a number. try again.
Your guess is too low of a number. try again.
Your guess is too low of a number. try again.
Your guess is too low of a number. try again.
Your guess is too low of a number. try again.
Your guess is too low of a number. try again.
Your guess is too low of a number. try again........
James Tursa
James Tursa 2020 年 4 月 7 日
Please post your current code.
Todd Wyzkiewicz
Todd Wyzkiewicz 2020 年 4 月 7 日
clear all
clc
r=randi(51)+49;
%the random integer generator will give a number inbetween 50 nad 100 for the user to guess.
disp('I''m thinking of a number between 50 and 100. Can you guess what it is?')
disp('I''m going to have you guess the number and I''ll let you know if it''s right.')
disp(' ')
disp('What is you''re guess')
user_number=input('Please input your number here: ');
clc
%laying the ground rules for the game, so it is clear and easy to understand.
disp('Here''s your guess:')
disp(user_number)
%Making the answer placement more appearent.
count=0;
numguess=0;
chance=0;
while r>=50 || r<=100
if user_number==r
disp('Nice job you guessed the right number!')
break
elseif user_number>r
disp('You guess is too high. try again.')
elseif user_number<r
disp('Your guess is too low of a number. try again.')
end
end
user_number=input('enter new guess here');
James Tursa
James Tursa 2020 年 4 月 7 日
編集済み: James Tursa 2020 年 4 月 7 日
Why did you move this line outside the loop?
user_number=input('enter new guess here');
It needs to be inside the loop. You had that part correct in your earlier code, but by moving it you made it incorrect. Move it inside the loop. E.g., just before the while end.
end
user_number=input('enter new guess here');
end
Todd Wyzkiewicz
Todd Wyzkiewicz 2020 年 4 月 7 日
wow. didnt even see that I moved that, but actually fixed my problem. thank you bringing that to light

サインインしてコメントする。

その他の回答 (2 件)

Walter Roberson
Walter Roberson 2020 年 4 月 6 日

0 投票

while r>=50 || r<=100
Suppose r is 73. Then r>=50 is true so the loop continues.
Suppose r is -6. Then r>=50 is false, but r<=100 is true, so the loop continues.
Suppose r is pi*10^8. Then r>=50 is true, so the loop continues.
The only way that your while loop will terminate is if the user enters NaN, or the user enters something that cannot be compared to numeric, such as a struct. (Or, very obscurely, some handle objects can be compared to numeric and the result will nearly always be 0.)

1 件のコメント

Todd Wyzkiewicz
Todd Wyzkiewicz 2020 年 4 月 7 日
My issue is that it only give the user one chance to answer. The donain isnt the problem the code is just cutting out before the right answer is given?

サインインしてコメントする。

Todd Wyzkiewicz
Todd Wyzkiewicz 2020 年 4 月 7 日

0 投票

could the issue be with the
count=0;
numguess=0;
chance=0;

4 件のコメント

James Tursa
James Tursa 2020 年 4 月 7 日
No, at least for the code you posted. You never use those variables in your code.
Todd Wyzkiewicz
Todd Wyzkiewicz 2020 年 4 月 7 日
do you know what those variables should be? I took those from my related notes, but Im not sure what they do for the code.
Todd Wyzkiewicz
Todd Wyzkiewicz 2020 年 4 月 7 日
the variable names deffinetly need to change. I took them from a something that is a while loop, but it had nothing to do with a guessing game. do you know if the numbers sould even start at 0?
James Tursa
James Tursa 2020 年 4 月 7 日
You don't use those variables, so you can delete those lines entirely and it won't make any difference to your code.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by