当我运行这个程序时,它会忽略switch
语句中的所有内容,并显示default
. 我正在使用代码::块。请向我解释为什么会遇到这个逻辑错误,也许它是 Code::Blocks 中的错误而不是代码中的错误——我应该在另一个 IDE 上尝试它吗?
#include <stdio.h>
int main()
{
double a,b;
int choose,subtract,divide;
printf("My first Calculater in c\n");
printf("\n");
printf("Enter your two numbers\n");
scanf("%lf",&a);
scanf("%lf",&b);
printf("Please choose an operation\n");
printf("1.add two no.(s)\n");
printf("2.subtract two no.(s)\n");
printf("3.multiply two no.(s)\n");
printf("4.divide two no.(s)\n");
printf("please choose an operation ");
scanf("%lf", &choose);
switch(choose){
case 1:
printf("sum of a+b is : %lf\n",(a+b));
break;
case 2:
printf("1.a-b?\n");
printf("2.b-a?\n");
scanf("%d", &subtract);
switch(subtract)
{
case 1:
printf("\ndiff of a-b is : %lf",(a-b));
break;
case 2:
printf("\ndiff of b-a is : %lf",(b-a));
break;
}
case 3:
printf("\nproduct of a*b is : %lf",(a*b));
break;
case 4:
printf("\n1.divide a/b?,a!=0");
printf("\n2.divide b/a?,b!=0");
scanf("%d", ÷);
switch(divide)
{
case 1:
if(b==0)
printf("\ndivision by zero is undefined");
else
printf("\nquotient of a/b is : %lf",(a/b));
break;
case 2:
if(a==0)
printf("\ndivision by zero is undefined");
else
printf("\nquotient of b/a is : %lf",(b/a));
break;
}
default:
printf("please choose either 1,2,3 or 4 options\n");
}
}
你正在双读 choose
scanf("%lf", &choose);
改为读取小数
scanf("%d", &choose);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句