新闻资讯

质量为本、客户为根、勇于拼搏、务实创新

< 返回新闻资讯列表

c语言猜数字1到100游戏怎么实现,c语言猜数字1到100游戏猜3次

发布时间:2023-08-16 07:52:03

c语言猜数字1到100游戏怎样实现

以下是一个实现C语言猜数字1到100游戏的示例代码:
```c
#include
#include
#include
int main() {
int number, guess, attempts = 0;
// 生成随机数
srand(time(NULL));
number = rand() % 100 + 1;
printf("欢迎参加猜数字游戏! ");
printf("猜一个介于1到100之间的数字。 ");
do {
printf("请输入你的猜想:");
scanf("%d", &guess);
attempts++;
if (guess > number) {
printf("猜大了! ");
} else if (guess < number) {
printf("猜小了! ");
} else {
printf("恭喜你,猜中了! ");
printf("你猜了 %d 次。 ", attempts);
}
} while (guess != number);
return 0;
}
```
这个程序的基本思路是:生成一个1到100之间的随机数,然后循环接受用户的输入猜想,根据用户的猜想输出相应的提示,直到用户猜中为止。程序记录了用户猜想的次数,并在猜中时输出猜想次数。