系统会随机产生一个数值,注释那句为随机数种子,
你也可以自己手动输入0到65535之间的数都行,
要是手动输入的话,有注释的那句换成:
unsigned rdm;
cin>>rdm;
srand(rdm);
随机数的范围给你限制到了2000以内,大了不好猜,也可以自己改,把那个2000换成你想要的范围n,范围就是0~n
#include<iostream>
#include<time.h>
#include<stdlib.h>
using namespace std;
void main()
{
srand((unsigned)time(NULL)); //系统随机产生一个数值
int real = (int)(2000.0 * rand() / (RAND_MAX + 1.0));
int guess;
int nCount = 10;
while (nCount != 0)
{
cout << "请输入你猜想的数据:";
cin >> guess;
nCount--;
if (guess < real)
cout << "猜测的数值小了,你还有" << nCount << "次机会" << endl;
else if (guess > real)
cout << "猜测的数值大了,你还有" << nCount << "次机会" << endl;
else
{
cout << "恭喜你答对了,你仅仅用了" << 10 - nCount << "次就猜对了" << endl;
nCount = 0;
break;
}
if (nCount == 0)
cout << "10次机会用完了,你仍然没有猜对,正确答案是:" << real << endl;
}
}
