运行正常,可以先打印出来验证,判断正确后应加句break退出for循环。
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void main()
{
int i,t,a[10]={0};
int m,counts=0;
srand(time(0));
for(i=0;i<10;i++)a[i]=0;
for(t=0;t<9876;)
{
i=rand()%10;
if(!a[i])
//这句可改成if(a[i]==0)
{
t=t*10+i;
a[i]++;
}
}
printf("%d game beginning\n",t);
printf("input five different numbers you guessed\n");
for(; ;)
{
scanf("%d",&m);
counts++;
if(m>t)
printf("Too high!\n");
else if(m<t)
printf("Too low!\n");
else
{
printf("right!
your score is %d\n",(100-counts+1));
printf("you guessed %d times\n",counts);
printf("right value is %d\n",t);
break;
//判断正确需要退出死循环
}
}
}