下面是一个简单的猜数字游戏的C++代码示例:

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

int main() {
    srand(time(0)); // 设置随机数种子

    int secretNumber = rand() % 100 + 1; // 生成1到100之间的随机数
    int guess;
    int tries = 0;

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

    do {
        std::cout << "请输入一个猜测的数字(1到100之间): ";
        std::cin >> guess;
        tries++;

        if (guess > secretNumber) {
            std::cout << "太大了!" << std::endl;
        } else if (guess < secretNumber) {
            std::cout << "太小了!" << std::endl;
        } else {
            std::cout << "恭喜你猜对了!" << std::endl;
            std::cout << "你用了 " << tries << " 次猜对。" << std::endl;
        }
    } while (guess != secretNumber);

    return 0;
}

这个游戏会生成一个1到100之间的随机数,玩家需要通过输入猜测的数字来猜测正确的数字。程序会根据玩家的猜测给出提示,直到猜测正确为止。游戏结束后,程序会显示玩家猜对的次数

用c++编一个游戏

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

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