confusion while; break; continue.

1 view (last 30 days)
Hello everyone, im starting with matlab, and i have a little problem if someone wants to help me it will be nice here is the problem:
A little guessing game works like this: The first player enters a number secret. The second player must guess this number. To do this, he enters a number and then the program tells him if the number to guess is greater or less than the number he suggested. he can keep entering numbers until the secret number is found. In this case, the program displays the number of tries that were required to find the secret number. If the number has not yet been found after 15 attempts, the number to guess is displayed and the program ends. Carry out this program. For interaction with the user, you can use the msgbox functions and inputdlg.
Well my code that im trying is here:
% Clean Up
% Close the opened figures and clear all variables
clear all;
close all;
% Open a dialog box asking the user 1 to put a number
secretnumber=str2double(inputdlg('Enter your secret number','secretnumber'));
%Open a dialog box asking the user 2 to put a number
number1=str2double(inputdlg('Enter your guess','number1'));
%If the number1>secretnumber show a msgbox saying it
while number1>secretnumber
uiwait(msgbox('Your guess is higher than the secret number, try again','modal'));
str2double(inputdlg('Enter your guess','number1'));
if number1<secretnumber
uiwait(msgbox('Your guess is lower than the secret number, try again','modal'));
str2double(inputdlg('Enter your guess','number1'));
break
end
end
here i am for now, i don't understand why when the value of number1 is smaller than the secretnumber; this code won't execute:
if number1<secretnumber
uiwait(msgbox('Your guess is lower than the secret number, try again','modal'));
str2double(inputdlg('Enter your guess','number1'));
break
end
Thanks a lot, if you have any hint for the msgbox to appear when the number1 is the same as the secret number, i'm taking it thanks.
  1 Comment
Blazhe Grkljanov
Blazhe Grkljanov on 18 Nov 2020
% Clean Up
% Close the opened figures and clear all variables
clear all;
close all;
% Open a dialog box asking the user 1 to put a number
secretnumber=str2double(inputdlg('Enter your secret number','secretnumber'));
%Open a dialog box asking the user 2 to put a number
number1=str2double(inputdlg('Enter your guess','number1'));
%If on the first try he got it right
if number1==secretnumber msgbox('good game 1 try')
end
%If the number1>secretnumber show a msgbox saying it
while number1~=secretnumber
% 1st time
if number1>secretnumber
uiwait(msgbox('Your guess is higher than the secret number, try again','modal'));
str2double(inputdlg('Enter your guess','number1'));
end
if number1<secretnumber
uiwait(msgbox('Your guess is lower than the secret number, try again','modal'));
str2double(inputdlg('Enter your guess','number1'));
end
end
I got until here; i don't know how to continue until 15th time, i tried to put msgbox ('you got the number after 1 try') after the last 'if' function in the loop , but whatever number i will put it will show the message box with the text inside so, what should i do to contiue until 15th times ?
Thanks a lot.

Sign in to comment.

Accepted Answer

Geoff Hayes
Geoff Hayes on 18 Nov 2020
Blazhe - I think the condition on your while loop is preventing you from entering the body (of the loop) and so preventing the code from checking that the guess is less than the secret number.
while number1>secretnumber
The above should probably be (not equal)
while number1 ~= secretnumber
and then you would need to adjust the code within the body to ensure that you properly handle all four cases: the guess is less than the secret number, the guess is greater than the secret number, the guess is equal to the secret number, and more than 15 guesses have been made.
  1 Comment
Blazhe Grkljanov
Blazhe Grkljanov on 18 Nov 2020
Thanks a lot, im trying to fix it. I changed the condition so it works now. I will try to do the other steps:
Thanks a lot for you quick response
:)

Sign in to comment.

More Answers (0)

Categories

Find more on Just for fun in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!