下面是一个用C语言实现的猜数字游戏程序,满足题目中的所有要求: ```c #include <stdio.h> int main() { int secretNumber, maxAttempts, userGuess, attempts = 0; // 读取随机数和最大尝试次数 scanf("%d %d", &secretNumber, &maxAttempts); while (1) { scanf("%d", &userGuess); if (userGuess < 0) { printf("game over\n"); break; } attempts++; if (userGuess == secretNumber) { if (attempts == 1) { printf("bingo!\n"); } else if (attempts <= 3) { printf("lucky you!\n"); } else if (attempts <= maxAttempts) { printf("good guess!\n"); } else { printf("game over\n"); } break; } else if (userGuess > secretNumber) { printf("too big\n"); } else { printf("too small\n"); } if (attempts >= maxAttempts) { printf("game over\n"); break; } } return 0; } ``` 代码说明: 1. 输入部分: - 首先读取两个整数:`secretNumber`(游戏机生成的随机数)和`maxAttempts`(最大尝试次数)。 - 然后在一个无限循环中读取用户的猜测`userGuess`。 2. 判断部分: - 如果用户输入的数是负数,输出`game over`并结束程序。 - 每次用户输入一个数,增加尝试次数`attempts`。 - 如果用户猜对了数字: - 如果是第一次猜对,输出`bingo!`。 - 如果在3次以内猜对,输出`lucky you!`。 - 如果在`maxAttempts`次以内猜对,输出`good guess!`。 - 如果超过了`maxAttempts`次猜对,输出`game over`。 - 如果用户猜错了数字,根据猜测的大小输出`too big`或`too small`。 - 如果尝试次数达到了`maxAttempts`,输出`game over`并结束程序。 示例输入输出: 假设输入如下: ``` 50 5 30 70 40 60 50 ``` 输出将是: ``` too small too big too small too big good guess! ``` 假设输入如下: ``` 50 3 30 70 -1 ``` 输出将是: ``` too small too big game over ``` 这个程序能够正确处理各种输入情况,并按照题目要求输出相应的结果。 [2024-10-31 09:15:00 | AI写代码神器 | 613点数解答]