C语言小游戏
时间: 2023-06-21 20:11:12 浏览: 29
好的,我可以介绍一个简单的 C 语言小游戏——猜数字游戏。
游戏规则:
1. 程序随机生成一个 1-100 的整数作为答案;
2. 玩家输入一个数字;
3. 如果玩家输入的数字等于答案,游戏结束,输出恭喜信息;
4. 如果玩家输入的数字小于答案,输出提示信息“你猜的数字太小了”;
5. 如果玩家输入的数字大于答案,输出提示信息“你猜的数字太大了”;
6. 玩家有 7 次机会猜测,如果 7 次都没有猜对,游戏失败,输出失败信息。
代码如下:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int answer, guess, chance = 7;
srand(time(NULL));
answer = rand() % 100 + 1;
printf("猜数字游戏开始!\n");
while (chance > 0)
{
printf("你还有 %d 次机会,请输入一个 1-100 的整数:", chance);
scanf("%d", &guess);
if (guess == answer)
{
printf("恭喜你,猜对了!\n");
break;
}
else if (guess < answer)
{
printf("你猜的数字太小了。\n");
}
else
{
printf("你猜的数字太大了。\n");
}
chance--;
}
if (chance == 0)
{
printf("很遗憾,你没有猜对,正确答案是 %d。\n", answer);
}
return 0;
}
```
希望能对你有所帮助!
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)