查看单个帖子
旧 2007-08-09, 22:19   #1
spy1120
普通会员
 
spy1120 的头像
 
注册日期: 2007-05-18
年龄: 38
帖子: 77
声望力: 19
spy1120 正向着好的方向发展
箭头 【文章】**编个猜数字的小游戏**

设计猜数字游戏:
首先让电脑产生一个〔1,100〕的随机数A,然后由用户猜测该数字A。如猜测的数字B大于A,则显示“High”,小于则显示“Low”,等于则显示“You Won”,同时退出游戏。用户最多可以猜7次。

程序:

%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'])
guess=input('Enter your guess:');
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
__________________
吾幼学文,10年不就,怒,遂习武,校场骑射,中鼓吏,乃逐,闭门研医良久,自攥一良方,服之,卒!!!
spy1120 当前离线   回复时引用此帖