以下是一个简单的猜数字游戏,使用C++编写:

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main() {
    srand(time(0)); // 随机数种子
    int number = rand() % 100 + 1; // 生成随机数
    int guess; // 玩家猜的数字
    int count = 0; // 猜测次数

    cout << "欢迎来玩猜数字游戏!" << endl;
    cout << "我已经想好了一个1到100之间的整数,请你猜一猜是多少。" << endl;

    do {
        cout << "请输入你猜测的数字(1-100):" << endl;
        cin >> guess;
        count++;

        if (guess > number) {
            cout << "猜大了,请再猜一次。" << endl;
        } else if (guess < number) {
            cout << "猜小了,请再猜一次。" << endl;
        }
    } while (guess != number);

    cout << "恭喜你,猜对了!你一共用了" << count << "次猜测。" << endl;

    return 0;
}

这个游戏会随机生成一个1到100之间的整数,然后让玩家猜这个数字是多少。玩家每输入一次猜测,程序会告诉他猜大了还是猜小了,直到玩家猜中为止。最后程序会告诉玩家猜测的次数。

用c++写一个小游戏

原文地址: https://www.cveoy.top/t/topic/bw1V 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录