永发信息网

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

答案:3  悬赏:80  手机版
解决时间 2021-02-06 15:22
#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);
}
最佳答案
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!
全部回答
没有死循环,是要运行十一次,正确代码如下:解释在代码上 #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); }
//--------------------------------------------------------------------------- #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("\ "); } //---------------------------------------------------------------------------
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
有谁听过(小医生)这个系列的化妆品吗? 安安生
什么是对刀块?石材雕刻机对刀块该如何使用?
某多层现浇钢筋混凝土框架结构房屋,9度抗震
什么茶降肺火
30多岁的女人体寒湿气重汗蒸时什么姿势最好
龙腾天下这个地址在什么地方,我要处理点事
金融工程和金融数学的区别
下楼用英语怎么说
以下关于黏液表皮样癌的叙述中,哪项是错误的
蕾特创业园我想知道这个在什么地方
城步苗族自治县公安局公路巡警大队地址有知道
小张野生鱼馆在哪里啊,我有事要去这个地方
向心力是向心运动还是离心运动?有没有公式证
我是1987年属兔未时 女朋友是1995年属猪辰时
请解释一下中医里的“腕腹胀满”的意思。是说
推荐资讯
请具体列式:高40m的瀑布,每秒流下的水量为1
下列结构中流着动脉血的是CA. 肺动脉、左心房
占用耕地修高速路用每年每亩1200元租用合理吗
老夏羊汤海鲜烧烤在哪里啊,我有事要去这个地
led显示屏颜色发暗淡无光
筑梦路上生男孩喜对联望赐教,共4副对联,多
康有为.梁启超发动的维新变法运动.其进步性在
男人喜欢清高的女人吗
扬声器总是无法调节音量大小怎么回事
飞利浦HTB3581/93 音响怎么链接飙歌达人
一枪球台球室在什么地方啊,我要过去处理事情
【陈寿三国志】《三国演义》所根据之史书是陈
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?