猜数字,谢谢大侠们

来源:百度知道 编辑:UC知道 时间:2024/06/04 13:04:45
首先随机生成一个1到100的整数,然后由人不断输入数字来猜这个数字的大小。猜错了,系统会给出一个提示,然后让用户继续猜。猜对了就退出程序。例如:随机生成的数是42,开始提示的范围是1~100,然后玩家猜是30,猜测是错误的,系统告诉你太小了。然后,玩家继续输入60,猜测依然错误,系统告诉你太大了。直到玩家猜到是42为止。用户最大的猜测次数是10次。

#include <iostream>
#include <ctime>
using namespace std;

int main(int argc,char*argv[])
{
int n,l;
srand((unsigned)time(0));
n=rand()%100+1;
cout <<"随机数已生成(1-100)...请输入你猜测的数字" <<endl;
for(int i=0;i<10;i++)
{
cin >>l;
if(l==n)
{
cout <<"恭喜你!猜对了!" <<endl;
break;
}
else if(l>n && i<9)
{
cout <<"太大了!请继续..." <<endl;
continue;
}
else if(l<n && i<9)
{
cout <<"太小了!请继续..." <<endl;
continue;
}
}
if(l!=n) cout <<"你已经没有机会了!" <<endl;

system("pause");
return 0;
}
DEV-C++运行通过