以下是一个基于C++的猜数字小游戏的示例代码:

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

int main() {
    srand(time(0)); // 使用当前时间作为随机数种子
    int secretNumber = rand() % 100 + 1; // 生成1到100之间的随机数
    int guess;
    int attempts = 0;

    std::cout << "欢迎来到猜数字游戏!" << std::endl;

    do {
        std::cout << "请输入一个1到100之间的整数: ";
        std::cin >> guess;
        attempts++;

        if (guess > secretNumber) {
            std::cout << "太大了!" << std::endl;
        } else if (guess < secretNumber) {
            std::cout << "太小了!" << std::endl;
        } else {
            std::cout << "恭喜你猜对了!" << std::endl;
            std::cout << "你总共猜了" << attempts << "次。" << std::endl;
            break;
        }
    } while (true);

    return 0;
}

这个小游戏使用rand()函数生成一个1到100之间的随机数作为秘密数字。然后,玩家可以输入一个猜测的数字,程序会根据玩家的猜测给出提示,直到玩家猜对为止。最后,程序会显示玩家猜对的次数。

请注意,以上代码只是一个简单的示例,可能还有一些错误处理和边界情况需要处理

请使用c++写一个小游戏

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

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