生成一个C语言的游戏代码
时间: 2024-05-15 09:12:29 浏览: 12
以下是一个简单的猜数字游戏的C语言代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int answer, guess, num_guesses = 0;
srand(time(NULL));
answer = rand() % 100 + 1;
printf("I'm thinking of a number between 1 and 100. Can you guess what it is?\n");
do {
printf("Enter your guess: ");
scanf("%d", &guess);
num_guesses++;
if (guess < answer) {
printf("Too low, try again.\n");
} else if (guess > answer) {
printf("Too high, try again.\n");
} else {
printf("Congratulations, you guessed correctly in %d guesses!\n", num_guesses);
}
} while (guess != answer);
return 0;
}
```
这个程序会生成一个1到100之间的随机数,然后让玩家猜这个数字,直到猜中为止。每次猜测后,程序会告诉玩家猜的数字是太大还是太小。如果玩家猜中了,程序会输出用了多少次猜测才猜中。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)