confusion while; break; continue.
Show older comments
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
on 18 Nov 2020
Accepted Answer
More Answers (0)
Categories
Find more on Loops and Conditional Statements 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!