美文网首页matlab学习
matlab 猜数字 (100以内)

matlab 猜数字 (100以内)

作者: Kerwin_H | 来源:发表于2019-06-21 09:32 被阅读0次

%猜数字 7次

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

相关文章

网友评论

    本文标题:matlab 猜数字 (100以内)

    本文链接:https://www.haomeiwen.com/subject/rrpeqctx.html