#include <iostream> #include <time.h> using namespace std; int main() { //生成随机数 //添加随机数种子,作用利用当前系统时间生成随机数,防止每次随机数都一样 srand((unsigned int)time(NULL)); int num=rand() % 100+1;//rand()%100+1生成0+1~99+1随机数 //玩家猜测 int a = 1; int cal=0; cin >> cal; while (1) { if (cal>num) { cout << "猜测大了" << endl; cin >> cal; } else if (cal<num) { cout << "猜测小了" << endl; cin >> cal; } else { cout << "猜测对了" << endl; break; } } system("pause"); return 0; }