【c语言】编写一个猜数字游戏

2024-11-27 02:22:16
推荐回答(4个)
回答1:

已经改正,如下:
#include
#include
#include
void circle(int i)
{
int n ;
printf("请输入您所猜的数字(0-100) \n");
scanf("%d",&n);
if(i {
printf("the number is larger,input agine.\n");
circle(i);
}

if(i>n)
{
printf("the number is smaller.input agine \n");
circle(i);
}

if(i==n)//?不是
printf("the number is %d so congratulations!you got it!",i);

}

int main()
{

int flag =1;

printf("This is a small game,what you should do is just guess a intange number.\n");
printf("Now,please guess the number!\n");
srand((unsigned)time(NULL));
while(flag)
{
int i=rand()%100;//?使用 % 而不是 / ,产生的数字在0-100之间,放在循环体内,每次循环都产生一个随机数
circle(i);

printf("\n继续玩请输入 1,退出请输入 0");
scanf("%d",&flag);
}

getchar();
return 0;

}

回答2:

/*编译工具:C-Free 4.0 已经编译通过*/
#include
#include
#include
void produce(int *,int* ); //电脑产生随机数和随机产生一个猜的次数
int judge(int ,int ); //判断猜对与否 猜对返回0 猜错返回正数或者负数

int main ()
{
int result,guess,count,a=0,b=100,n;
produce(&result,&count); //首先由电脑产生一个随机数并得到猜的次数
n=count;
while(count)
{
printf("You still have %d chances left!\n",count-1);
printf("Pease input your guess:");
scanf("%d",&guess);
if(judge(result,guess)>0)
{
a=guess; //太小了 更新猜数范围的下限
printf("太小了!\n新的范围:%d-%d\n",a,b);
}
else if(judge(result,guess)<0)
{
b=guess; //太大了 更新猜数范围的上限
printf("太大了!\n新的范围:%d-%d\n",a,b);
}
else
break;
count--;
}
if(count>0)
printf("Congratulation! your guess is right!\n");
else
printf("天啊%d次你都猜错了。杯具。。。",n);
return 0;
}
void produce(int *result,int* count)
{
srand(time(NULL));
*result=rand()%100; //产生0-100之间的数
*count=rand()%10+3; //产生3-12之间的数来代表你猜数的机会
}
int judge(int result,int guess)
{
return (result-guess);
}

回答3:

scanf("%d",n);
又是这里的问题 &
你要不加这个符号,系统给赋的值肯定是一个很小的负数,所以是这种结果了

你要进行多次猜的话
void main()
{
int i,n;
int j=0;
srand((unsigned)time(NULL));

printf("%d\n",i);
printf("This is a small game,what you should do is just guess a intange number.\n");
printf("Now,please guess the number!\n");
while(j<100)
{
i=rand()%100;
while(i!=n)
{ scanf("%d",&n);
circle(n);
}
j++;
}
getch();

}
嵌套循环不就可以了

回答4:

不懂程序 无能为力~@~