C++——猜数游戏

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

int main()
{
	//添加随机数种子,利用当前时间来生成随机数,防止都一样
	srand((unsigned int)time(NULL));

	//1 系统生成随机数
	int num = rand() % 100+1;//rand() 随机生成数字,后面%100,表示范围 0~99
	cout << num << endl;

	//2 玩家进行猜测
	int val = 0;

	while (1)
	{
		cout << "请输入你猜的数字:";
		cin >> val;

		if (val > num)
		{
			cout << "你猜的数据过大" << endl;
		}
		else if (val < num)
		{
			cout << "你猜的数据过小" << endl;
		}
		else
		{
			cout << "你猜对了!" << endl;
			break;
		}
	}

	system("pause");

	return 0;
}