以下是一个用 C 语言实现猜数字游戏的示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <time.h> int main() { int target, guess; srand(time(NULL)); // 初始化随机数生成器 target = rand() % 10 + 1; // 生成 1 到 10 之间的随机数作为目标数字 printf("猜一个介于 1 到 10 之间的数字: "); scanf("%d", &guess); while (guess!= target) { if (guess > target) { printf("太大了!\n"); } else { printf("太小了!\n"); } printf("再猜: "); scanf("%d", &guess); } printf("恭喜你,猜对了!\n"); return 0; } ``` 这段代码首先生成一个 1 到 10 之间的随机数作为要猜测的目标数字,然后让用户输入猜测的数字,根据用户输入与目标数字的比较给出提示,直到用户猜对为止。 [2024-11-03 19:41:46 | AI写代码神器 | 265点数解答]