猜数字游戏代码C语言
时间: 2023-12-07 09:38:23 浏览: 48
以下是一个简单的猜数字游戏的C语言代码,具体实现过程如下:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int guess, num, count = 0;
srand((unsigned)time(NULL));
num = rand() % 100 + 1; // 生成1-100之间的随机数
printf("猜数字游戏开始!\n");
do {
printf("请输入你猜的数字(1-100):");
scanf("%d", &guess);
count++;
if (guess > num) {
printf("猜大了!\n");
} else if (guess < num) {
printf("猜小了!\n");
} else {
printf("恭喜你,猜对了!\n");
printf("你一共猜了%d次。\n", count);
break;
}
} while (1);
return 0;
}
```
相关问题
猜数字游戏c语言代码
猜数字游戏的C语言代码可以如下所示:
```c
#include <stdio.h>
int main() {
int n = 0; // 玩家输入的数字
int ans = 7; // 这是事先设置好的目标数字
printf("请输入一个0 - 9之间的数字:");
scanf("%d", &n);
do {
if (n > ans) {
printf("猜大了!\n");
} else if (n < ans) {
printf("猜小了!\n");
}
} while (n != ans);
printf("恭喜你,猜对了!\n");
return 0;
}
```
c语言猜数字游戏代码
以下是C语言猜数字小游戏的代码实现:
```
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int num, guess, tries = 0;
srand(time(0));
num = rand() % 100 + 1;
printf("猜数字游戏\n");
do
{
printf("请输入一个1到100之间的整数:");
scanf("%d", &guess);
tries++;
if (guess > num)
{
printf("太大了!\n");
}
else if (guess < num)
{
printf("太小了!\n");
}
else
{
printf("恭喜你猜对了!你用了%d次机会。\n", tries);
}
} while (guess != num);
return 0;
}
```
相关推荐
![](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)