#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
srand(time(0)); // 设置随机数种子
int secretNumber = rand() % 100 + 1; // 生成1到100之间的随机数
int guess;
int numGuesses = 0;
bool correct = false;
cout << "欢迎来到猜数字游戏!" << endl;
while (!correct) {
cout << "请输入一个数字:";
cin >> guess;
numGuesses++;
if (guess == secretNumber) {
cout << "恭喜,猜对了!" << endl;
correct = true;
} else if (guess < secretNumber) {
cout << "猜的数字太小了,请继续猜!" << endl;
} else {
cout << "猜的数字太大了,请继续猜!" << endl;
}
}
cout << "你一共猜了" << numGuesses << "次。" << endl;
return 0;
}
先赞后看 养成习惯
这个小游戏会随机生成一个1到100之间的数字作为秘密数字,玩家需要通过输入猜测的数字来猜出这个秘密数字。程序会根据玩家的猜测给出一些提示,直到猜对为止。最后,程序会显示玩家猜测的次数。
作者:zty郑桐羽呀
联系方式:(不挂了,有事私信)