#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void create_secret(int *c)
{
srand(time(NULL));
*c = rand()%100 + 1;
}
void guess(int n)
{
int inputnumber, times = 1;
while(1){
printf("enter a number:\n");
scanf("%d", &inputnumber);
if(inputnumber == n){
printf("Get it by %d\n", times);
break;
}
if(inputnumber > n){
printf("too high\n");
}
else{
printf("too low\n");
}
times++;
}
}
int main(void)
{
int n;
create_secret(&n);
guess(n);
return 0;
}
/*
enter a number:
66
too high
enter a number:
55
too high
enter a number:
30
too low
enter a number:
40
too high
enter a number:
36
too high
enter a number:
32
too low
enter a number:
33
Get it by 7
*/猜数:首先生成一个介于1-100之间的整数,从键盘不断输入数值直到该数值与生成的那个数相符为止,并打印出猜数的次数。
最新推荐文章于 2022-08-20 18:34:37 发布
本文介绍了一个简单的猜数字游戏程序,使用C语言编写。程序首先生成一个1到100之间的随机数作为目标数字,然后提示用户输入猜测的数字。根据用户的输入与目标数字的比较,给出太高或太低的反馈,直到用户猜中为止。记录了用户猜测的次数并在猜中后显示。
471

被折叠的 条评论
为什么被折叠?



