永发信息网

C语言问题 为什么会有死循环 代码如下

答案:3  悬赏:80  手机版
解决时间 2021-02-06 15:22
  • 提问者网友:無理詩人
  • 2021-02-06 03:58
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
void main()
{
int a,b,c,d,f;
float e;
e=0;
f=0;
srand(time(NULL));
a=rand()%10+1;
b=rand()%10+1;
c=rand()%4;
for(f=0;f<=10;f++)
{
switch(c)
{
case 0:
{
printf("%d+%d=?\n",a,b);
scanf("%d\n",&d);
if(d==a+b)
{
printf("Right!\n");
e++;
}
if(d!=a+b)
{
printf("Wrong\n");
}
break;
}
case 1:
{
printf("%d-%d=?\n",a,b);
scanf("%d\n",&d);
if(d==a-b)
{
printf("Right!\n");
e++;
}
if(d!=a-b)
{
printf("Wrong\n");
}
break;
}
case 2:
{
printf("%d*%d=?\n",a,b);
scanf("%d\n",&d);
if(d==a*b)
{
printf("Right!\n");
e++;
}
if(d!=a*b)
{
printf("Wrong\n");
}
break;
}
case 3:
{
printf("%d/%d=?\n",a,b);
scanf("%d\n",&d);
if(d==a/b)
{
printf("Right!\n");
e++;
}
if(d!=a/b)
{
printf("Wrong\n");
}
break;
}
}
}
printf("总分数是:%f\n",e*10);
printf("正确率是:%f\n",e/10);
}
最佳答案
  • 五星知识达人网友:神鬼未生
  • 2021-02-06 05:06
a=rand()%10+1;
b=rand()%10+1;
c=rand()%4;
这三句应该放到for循环中,不然,for循环中,abc三个数,不会发生变化!!
for(f=0;f<=10;f++)
{
    a=rand()%10+1;
    b=rand()%10+1;
    c=rand()%4;
    switch(c)
    { 另:
scanf("%d",&d);  //这里输入时,不应该有\n!
全部回答
  • 1楼网友:老鼠爱大米
  • 2021-02-06 06:36
没有死循环,是要运行十一次,正确代码如下:解释在代码上 #include<stdio.h> #include<time.h> #include<stdlib.h> void main() {  int a,b,c,d,f;  float e;  e=0;  f=0;  srand(time(NULL));  //a=rand()%10+1; 放到循环中    //b=rand()%10+1; 放到循环中    //c=rand()%4;放到循环中     for(f=0;f<=9;f++)//循环十次  {   a=rand()%10+1;   b=rand()%10+1;      c=rand()%4;   switch(c)   {   case 0:    {     printf("%d+%d=?\n",a,b);     scanf("%d",&d);//不要有\n     if(d==a+b)     {      printf("Right!\n");      e++;     }     if(d!=a+b)     {      printf("Wrong\n");     }     break;    }   case 1:    {     printf("%d-%d=?\n",a,b);     scanf("%d",&d);//不要有\n     if(d==a-b)     {      printf("Right!\n");      e++;     }     if(d!=a-b)     {      printf("Wrong\n");     }     break;    }   case 2:    {     printf("%d*%d=?\n",a,b);     scanf("%d",&d);//不要有\n     if(d==a*b)     {      printf("Right!\n");      e++;     }     if(d!=a*b)     {      printf("Wrong\n");     }     break;    }   case 3:    {     printf("%d/%d=?\n",a,b);     scanf("%d",&d);//不要有\n     if(d==a/b)     {      printf("Right!\n");      e++;     }     if(d!=a/b)     {      printf("Wrong\n");     }     break;    }   }  }  printf("总分数是:%f\n",e*10);  printf("正确率是:%f\n",e/10); }
  • 2楼网友:廢物販賣機
  • 2021-02-06 05:13
//--------------------------------------------------------------------------- #include "stdio.h" #include "stdlib.h" #include "string.h" void main() { int i,t=1,n; char m[10]; struct st {char name[10]; int number; struct st *next; }*p,*head,*q; head=(struct st*)malloc(sizeof(struct st)); p=q=head; printf("enter name and numer:"); scanf("%[^,]%*c%d",&m,&n); strcpy(p-&gt;name ,m); p-&gt;number=n; fflush(stdin); if((strlen(m))==0||n==0)t=0; while(t) {printf("enter name and number:"); scanf("%[^,]%*c%d",&m,&n); fflush(stdin); if((strlen(m))==0||n==0)break; p=(struct st*)malloc(sizeof(struct st)); strcpy(p-&gt;name ,m); p-&gt;number=n; q-&gt;next=p; q=p; } p-&gt;next=null; p=head; while(p!=null) { printf("%s,%d\ ",p-&gt;name,p-&gt;number); p=p-&gt;next; } printf("\ "); } //---------------------------------------------------------------------------
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯