陷入困境,while无休止循环
#include <stdlib.h>#include <time.h>
main()
{
int a[4],b[4];
int i,j,k,count=0;
int rightposition=0,rightdigit=0;
srand(time(NULL));
a[0]=rand()%10;
do
{
a[1]=rand()%10;
}while(a[0]==a[1]);
do
{
a[2]=rand()%10;
}while(a[0]==a[2]||a[1]==a[2]);
do
{
a[3]=rand()%10;
}while(a[0]==a[3]||a[1]==a[3]||a[2]==a[3]);
printf("how time do you want to try:\n");
scanf("%d",&k);
do
{
count++;
do
{
printf("input four diffrent numbers:\n");
scanf("%d%d%d%d",&b[0],&b[1],&b[2],&b[3]);
}while(b[0]==b[1]||b[0]==b[2]||b[0]==b[3]||b[1]==b[2]||b[1]==b[3]||b[2]==b[3]);
for(i=0;i<4;i++)
{if(a[i]==b[i]) rightposition++;}
for(i=0;i<4;i++)
for(j=0;j<4;j++)
if(a[i]==b[j]&&i!=j) rightdigit++;
}while(rightposition!=4&&count!=k); //这一步一直循环,不知道什么原因
if(rightposition==4)
printf("congtatulation!\n");
else
printf("game over!\n");
for(i=0;i<4;i++)
printf("%d",a[i]);
}