题目要求:输入一个四位数,和电脑产生的一个四位数字相比较,这里假设是1352。如果数字相等且位置一样,用A表示;若数字相同位置不同用B表示。例如输入1234,结果输出1A2B。下面的程序经过改过之后的,我希望在“do you want to continue " 后输入'y'在继续执行~~~可是不知道如何返回~~
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <process.h>
void main()
{
int answer[4],i,j,guess=0,a,b,times=0;
char c;
for(i=0;i<4;i++)
{
srand((unsigned)time(NULL));
answer[i]=rand()%10;
for(j=0;j<i;j++)
{
while(answer[i]==answer[j])
{
answer[i]=rand()%10;
j=-1;
}
}
}
printf("Please guess:\n");
while(a!=4)
{
a=0;
b=0;
times++;
do
{
printf("%-4d",times);
scanf("%d",&guess);
if(guess==0)
exit(0);
}while(guess<0||guess>9999);
for(i=3;i>-1;i--)
{
for(j=0;j<4;j++)
if((int)(guess/pow(10,i))==answer[j])
{
if(i+j==3)
a=a+1;
else
b=b+1;
}
guess=guess-(int)(guess/pow(10,i))*pow(10,i);
}
printf(" %dA%dB\n",a,b);
}
printf("congratulation to you!\n");
printf("you take %d times to guess right!\n",times);
printf("this time generated number is:\n");
for(i=0;i<4;i++)
printf("%3d",answer[i]);
printf("\n");
printf("do you want to continue(y/n)?");
scanf("%c",&c);
if(c=='y')
返回程序重新开始;//关键是这里如何返回,我用goto语句好像不行啊
else
exit(0);
}
[此贴子已经被作者于2005-12-5 18:10:59编辑过]