/*今天用C++语言写一个猜数字的小游戏
*游戏规则:
*1.由系统随机产生一个1-100之间的数字
*2.用户在窗口上输入一个数字
*3.判断用户输入的数字和系统产生的数字(用户输入的数字大、小或相等)
*4.用户输入数字的机会共有7次
*/
#include <iostream>//类似C语言中的stdio.h这个头文件
#include <ctime>//包含时间头文件
using namespace std;//使用std名字空间
int main()
{
const int cnLimit = 7;//给7次猜数字的机会
int nSystem, nUser, nCount = 1;
//system("color E0");
//1.由系统随机产生一个1 - 100之间的数字
srand((unsigned int)time(0));//初始化随机种子
nSystem = rand() % 100 + 1;
//要是20-101之间的随机数,怎么弄
//rand() % (101 - 20 + 1) + 20;
//2.用户在窗口上输入一个数字
//3.判断用户输入的数字和系统产生的数字(用户输入的数字大、小或相等)
//4.用户输入数字的机会共有7次
while (true)//bool 它的值共两种,一个true, 一个false
{
cout << "请输入你第" << nCount << "次猜的数字:";
cin >> nUser;
if (nUser == nSystem)
{
cout << "恭喜!你在第" << nCount << "次时猜的数字正确!" << endl;
break;
}
else if (nUser < nSystem)
cout << "你猜的数字小了!" << endl;
else
cout << "你猜的数字大了!" << endl;
cout << "--------------------------------------------" << endl;
if (nCount++ == cnLimit)
{
cout << "你没有机会了,正确的数字是" << nSystem << "." << endl;
break;
}
}
//cout << "系统产生的随机数是:" << nSystem << endl;//endl end line 相当于printf("\n");
return 0;
}