c语言代码小游戏
时间: 2023-07-27 22:05:57 浏览: 39
好的,我可以帮你写一个简单的猜数字小游戏的 C 语言代码。代码如下:
```c
#include <stdio.h>
#include <stdlib.h> // 包含随机数生成函数需要的头文件
#include <time.h> // 包含 time 函数需要的头文件
int main()
{
int guess, answer, count = 0;
// 生成随机数,范围为 1~100
srand(time(NULL));
answer = rand() % 100 + 1;
// 猜数字过程
do {
printf("请猜一个1~100之间的整数:");
scanf("%d", &guess);
count++;
if (guess > answer) {
printf("您猜大了!\n");
} else if (guess < answer) {
printf("您猜小了!\n");
}
} while (guess != answer);
// 猜对后输出结果
printf("恭喜您猜对了!总共猜了%d次。\n", count);
return 0;
}
```
这是一个简单的猜数字小游戏,程序会随机生成一个 1~100 之间的整数,用户需要猜出这个数字,程序会提示用户猜大了或者猜小了,直到用户猜对为止。同时,程序还会记录用户一共猜了多少次。