c语言猜数问题,位置和数字都对记为A,数字对位置错记为B,希望各位大侠伸出援手,帮我一下哈,看看这个程序

2024-12-18 07:03:07
推荐回答(3个)
回答1:

先随机获得4个不同的一位数,如果不是则退出程序,如果获得成功,则用户再输入4个数,允许输入十次,好多判断之后,如果全对则你猜对了,否则你猜错了.不过这里有些小错误和不足哦
我帮你修改了一下

#include
#include
#include
int main(void)
{
int a,b,c,d,e,f,g,h,A,B,t=0;
srand(time(NULL));
a=rand()%10;
b=rand()%10;
c=rand()%10;
d=rand()%10;
if((a!=b)&&(a!=c)&&(a!=d)&&(b!=c)&&(b!=d)&&(c!=d))/* ; 这里不要*/
{
do{
printf("please input four different numbers:");
scanf("%1d%1d%1d%1d",&e,&f,&g,&h);/* 里面的 \n 是不能随便加的,因为如果加了的话,你最后必须多输入一个回车键才可以*/
A=0,B=0;
t++;
if(a==e)/*注意下面else不能丢啊*/
A++;
else if((b==e)||(c==e)||(d==e))
B++;
if(b==f)
A++;
else if((a==f)||(c==f)||(d==f))/*这里你原来是不是写错了呢?*/
B++;
if(c==g)
A++;
else if((a==g)||(b==g)||(d==g))
B++;
if(d==h)
A++;
else if((a==h)||(b==h)||(c==h))
B++;
printf("您完全猜对了%d个 ^_^\n有%d个对,但位置不对\n",A,B);
}while ((A!=4)&&(t<=10));
if(A=4)
{
printf("you are ringt");
}
else(h>=10);
{
printf("you are lost");
}
}
else /*这里还是加个吧,不然有时 直接退出 会让人莫名其妙的*/
printf("系统猜错了,就算你赢了吧\n\n\n");

return 0;//要有返回值
}

回答2:

1. 如果 abcd 出现相等了 你的程序好像并没有返回重新生成4个随机数吧
2. if((a==f)||(a==f)||(a==f)) 这句应该是(a==f)||(b==f)||(c==f))吧
3. while ((A!=4)&&(t<=10));
printf("you are right"); (printf这一句是多余的吧)
4. else(h>=10); (应该是t>=10吧)
{
printf("you are lost");
}
其他的暂时没有发现问题
机器上没有C编译器 没有执行
你在调试的过程中 可以先把 e f g h 打印出来 方便你调试
输出结果几A几B的时候可以显示成 XA XB 的形式 更方便看

回答3:

#include
#include
#include
main()
{

int a,b,c,d,e,f,g,h,A,B,t=0;

srand(time(NULL));
a=rand()%10;
b=rand()%10;
c=rand()%10;
d=rand()%10;

if((a!=b)&&(a!=c)&&(a!=d)&&(b!=c)&&(b!=d)&&(c!=d))
{

do{
printf("please input four different numbers:\n");
scanf("%d,%d,%d,%d",&e,&f,&g,&h);
getchar();
printf("%d,%d,%d,%d\n",e,f,g,h);
A=0,B=0;
t++;
if(a==e)
A++;
if((b==e)||(c==e)||(d==e))
B++;
if(b==f)
A++;
if((a==f)||(c==f)||(d==f))
B++;
if(c==g)
A++;
if((a==g)||(b==g)||(d==g))
B++;
if(d==h)
A++;
if((a==h)||(b==h)||(c==h))
B++;
printf("A:%d,B:%d\n",A,B);
}
while ((A!=4)&&(t<=3));

if(A==4)
{
printf("you are ringt\n");
}
else(t>3);
{
printf("you are lost\n");
}
}
}