#include<iostream>
using namespace std;
void myGuess() {
int s = rand() % 200 + 1;//利用取余随机生成1~200的数字
int c;
cout << "你猜测的数是(1~200)" << endl;
int t = 0;//猜测的次数
while (t < 10) {
cin >> c;
if (s==c) {
cout << "猜对了,游戏结束!" << endl;
cout << "你一共猜了" << t + 1 << "次" << endl; break;//因为t++在循环后面,因此这个t需要加上1
}
else {
if (c < s)
cout << "猜错了,太小了" << endl;
} {if (c > s)
cout << "猜错了,太大了" << endl; }
t++;
if (t == 10)
cout << "你的机会已用完,游戏结束!" << endl;
else
cout << "你还有" << 10 - t << "次机会" << endl;
continue;
}
}
int main() {
srand((unsigned int)time(NULL));//创建随机种子 利用当前系统时间生成随机数,防止每次随机数都一样
cout << "**************************" << endl;
cout << "********1、开始游戏*******" << endl;
cout << "********0、结束游戏*******" << endl;
cout << "**************************" << endl;
int select;
cout << "请输入你的选择:" << endl;
cin >> select;
switch (select) {
case 1:myGuess();
return 0;
case 0:cout << "退出成功!" << endl;
exit(0);
default:cout << "输入有误,请重新输入!" << endl; break;
}
return 0;
}