
规则:首先由计算机随机产生0-100之间的随机整数,然后由用户猜测所产生的随机数。根据用户猜测的情况给出不同的提示,如猜测的数大于产生的数,则显示“High”,小于则显示“Low”,等于则显示“You won”,同时退出游戏,用户最多可猜测7次。
clear all; clc; % play the game of guess the number x = fix(100 * rand); % a random number calculated by the computer n = 7; test = 1; for k = 1:7 numb = int2str(n) ; disp(['You have a right to ', numb, ' guesses']); disp('A guess is a number between 0 and 100'); disp(' '); guess = input('Enter your guess: '); if isempty(guess) disp('Please enter a number!'); break; end if guess < x disp('Low') ; elseif guess > x disp('High'); else disp('You won!'); test = 0; break; end n = n - 1; end if test == 1 disp('You lost!'); end
查看评论