gpt4 book ai didi

c - 为什么 switch 语句在这个 C 计算器中显示默认值?

转载 作者:行者123 更新时间:2023-11-30 14:47:59 24 4
gpt4 key购买 nike

当我运行这个程序时,它会忽略 switch 语句中的所有内容,并显示 default。我正在使用 Code::Blocks。请向我解释为什么会遇到这个逻辑错误,也许这是 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", &divide);
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");
}

}

最佳答案

您正在双读选择

scanf("%lf", &choose);

改为读取小数

scanf("%d", &choose);

关于c - 为什么 switch 语句在这个 C 计算器中显示默认值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50827471/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com