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

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

int main() {
    srand(time(NULL)); // 设置随机数种子
    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;
}

这个程序会生成一个1到100之间的随机数,然后要求用户输入一个数进行猜测。如果用户猜的数比随机数大,则提示太大了;如果用户猜的数比随机数小,则提示太小了;如果用户猜对了,则输出恭喜消息和猜测的次数

请帮我用c++写一个mc

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

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