我编写了这个非常简单的代码来查找电阻值。代码将编译并询问初始问题,但是当输入 P 或 S 时,代码崩溃并退出。任何帮助都会很棒,我知道这将是我错过的非常简单的事情......
#include <stdio.h>
void main ()
{
float res1;
float res2;
float res3;
float answer;
char calctype;
printf("Please enter 1st resistor value:");
scanf("%f", &res1);
printf("Enter 2nd resistor value:");
scanf("%f", &res2);
printf("Enter 3rd resistor value:");
scanf("%f", &res3);
puts("type P for Parallel calculation or S for Series calculation:\n");
scanf("%c", calctype);
if (calctype == 'S') {
answer = res1 + res2 + res3;
printf("The Series value is:%f \n", answer);
}
else if (calctype == 'P') {
answer = 1/(1/res1 + 1/res2 + 1/res3);
printf("The Parallel Value is:%f \n", answer);
}
}
谢谢!