% This function implements a simple guessing game where the user % picks a number and the computer must guess it. function guess2 % Display a message: disp('Think of a number between 1 and 100 and then hit any key.'); % Wait for keypress. pause; % Loop forever: while 1 % Make a random guess between 1 and 100 myguess = randi(100); % Create a prompt using string formatting function. prompt = sprintf('Is it %d? (y/n) ', myguess); % Prompt the user for an answer and store it in a string. answer = input(prompt, 's'); % See if the string matches 'y': if strcmpi(answer, 'y') % If correct, exit the loop using "break": disp('I win!') break else % Otherwise, keep going. disp('Ok, I''ll keep guessing.') end end