开关无法正常工作,不确定为什么,谢谢Cheerz忽略可能的错误数学生病,稍后将其弄清楚,否则您也可以修复它:P如果我放置C或C前两个输入似乎可以正常
#include <stdio.h>
#include <conio.h>
#include <math.h>
int main()
{
int decider;
double radius, height, circumference, surfaceArea, volume;
const double PI = 3.14159265;
printf("please enter radius in cm:");
scanf("%lf", &radius);
printf("please enter height in cm:");
scanf("%lf", &height);
printf("C. Calculate and display the circumference of the base of the conen"
"S. Calculate and display the surface area of the conen"
"V. Calculate and display the volume of the conen");
scanf("%d", &decider);
switch(decider)
{
case 'c':
case 'C':
circumference = (2.0f*PI)*radius;
printf("%lf cm", circumference);
break;
case 's':
case 'S':
surfaceArea = PI * (pow(radius, 2)) + (PI * radius) * (sqrt((pow(height, 2)) + (pow(radius, 2))));
printf("%lf cm sqaured", surfaceArea);
break;
case 'v':
case 'V':
volume = PI * (pow(radius, 2)) * (height/3.0f);
printf("%lf cm cubed", volume);
break;
default:
printf("An invalid option was selected!");
}
getch();
return 0;
}
变量decider
具有类型int
,并且您使用的是%d
格式指定符以读取值,该值期望将小数点整数作为输入。因此,除非您输入一个给定字母之一的ASCII代码的数字,否则您将始终转到默认值。
您正在寻找角色,因此请改用decider
A char
,然后使用%c
读取它:
char decider;
...
scanf(" %c", &decider);
还请注意%c
之前的空间。该空间将吸收任何空间字符,包括完成最后一行文本的新线。这是%c
所需的,因为与其他格式指定器不同,%c
将读取和存储一个withespace字符。