```c #include <stdio.h> #include <stdlib.h> #include <time.h> int main() { int target, guess; // 设置随机数种子 srand(time(0)); // 生成1~10的随机数 target = rand() % 10 + 1; printf("猜一个1~10之间的数字:\n"); do { scanf("%d", &guess); if (guess > target) { printf("太大了!\n"); } else if (guess < target) { printf("太小了!\n"); } else { printf("恭喜你猜对了!\n"); } } while (guess != target); return 0; } ``` 代码逻辑说明: 1. 使用`srand(time(0))`初始化随机数种子,确保每次运行程序生成的随机数不同 2. `rand() % 10 + 1`生成1到10之间的随机整数作为目标值 3. 使用`do-while`循环确保至少执行一次猜测 4. 通过比较用户输入与目标值给出相应提示 注意事项: - 需要包含`stdlib.h`和`time.h`头文件以使用随机数函数 - 用户输入非数字时程序会出错,实际应用中应添加输入验证 - 可以添加猜测次数限制来增加游戏难度 [2025-05-21 16:35:09 | AI写代码神器 | 297点数解答]