设计一个猜数字游戏,即系统随机出一个0-100的整数,用户输入数字,提示输出大还是小,超过
4次则为用户失败,游戏结束后询问用户是否下一轮。
#include#include #include using namespace std; int main(void) { srand((unsigned int)time(NULL));//生成随机数 int sum; sum = rand() % 1000; int a = 0; int i = 0; cout << "请猜一下这个数是多少" << endl; while (cin >> a) { i++; if (a > sum) cout << "猜大了n" << endl; else if (a <= sum / 2) cout << "猜小了n" << endl; else if (a < sum && a >= sum / 2) cout << "再大一点n" << endl; else if (a == sum) { cout << "猜对了n" << endl; break; } if (i >= 4) { cout << "你猜错了4次,游戏结束,你输了。" << endl; string str; cout << "是否重新开始游戏? 是/否" << endl; cin >> str; if (str == "是") { system("cls"); cout << "请猜一下这个数是多少" << endl; i = 0; } else if(str == "否") break; } } system("pause"); return 0; }



