出bug了
如果输入不合法的算式,就会循环打印:"请输入一个算式:",’"叮咚!输入算式不合法,请重新输入:)"大家帮看看是什么问题{:10_266:} #include <stdio.h>
int main()
{
while(1)
{
float numa=0,numb=0,numc=0;
char numd=0;
printf("请输入一个算式:\n");
scanf("%f%c%f",&numa,&numd,&numb);
switch(numd)
{
case '+':printf("结果为%f\n",numc=numa+numb);break;
case '-':printf("结果为%f\n",numc=numa-numb);break;
case '*':printf("结果为%f\n",numc=numa*numb);break;
case '/':printf("结果为%f\n",numc=numa/numb);break;
default:printf("叮咚!输入算式不合法,请重新输入:)");break;
}
}
return 0;
} 试试这样:#include <stdio.h>
int main()
{
while (1)
{
float numa = 0, numb = 0, numc = 0;
char numd = 0;
printf("请输入一个算式:\n");
fflush(stdin); // 添加了这行
scanf("%f%c%f", &numa, &numd, &numb);
switch (numd)
{
case '+':
printf("结果为%f\n", numc = numa + numb);
break;
case '-':
printf("结果为%f\n", numc = numa - numb);
break;
case '*':
printf("结果为%f\n", numc = numa * numb);
break;
case '/':
printf("结果为%f\n", numc = numa / numb);
break;
default:
printf("叮咚!输入算式不合法,请重新输入:)");
break;
}
}
return 0;
} ckblt 发表于 2022-2-9 14:59
试试这样:
谢谢大佬,之前没注意
页:
[1]