当然,我可以帮你编写一个简单的猜数字游戏。以下是一个使用C语言编写的示例代码:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main() {
    int secretNumber, guess, attempts = 0;
    srand(time(0));  // 初始化随机数生成器
    secretNumber = rand() % 100 + 1;  // 生成1至100之间的随机数

    printf("=== 猜数字游戏 ===
");
    printf("我生成了一个1至100之间的秘密数字,请猜一猜是多少。
");

    do {
        printf("请输入你的猜测:");
        scanf("%d", &guess);
        attempts++;

        if (guess > secretNumber) {
            printf("太大了!再试一次。
");
        } else if (guess < secretNumber) {
            printf("太小了!再试一次。
");
        } else {
            printf("恭喜你猜对了!
");
            printf("你猜了%d次。
", attempts);
        }
    } while (guess != secretNumber);

    return 0;
}

这个猜数字游戏会生成一个随机的秘密数字,然后玩家需要根据提示输入猜测的数字,直到猜中为止。每次猜测后,程序会告诉玩家猜测的数字是太大还是太小,直到最终猜中为止,并显示玩家猜测的次数。

你可以根据自己的需要进行修改和扩展,例如增加更多的关卡、增加计分系统或增加其他的游戏规则。祝你玩得开心!

C语言猜数字游戏代码:简单易懂,轻松上手

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

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