坚持课程中最简单的部分,编写要求您指定要使用的元素的代码(必须是一个或两个)。当我输入1时,它打印正确的东西。当我输入2时,它会打印else语句下的内容。为什么会这样?
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main() {
int nElements;
double element1, element2;
printf("\n Input number of flow elements to use (1 or 2): ");
scanf("%lf", &nElements);
if (nElements == '1') {
printf("\n Specify type of element (1 for freestream, 2 for source, 3 for vortex): ");
scanf("%lf", &element1);
} else
if (nElements == '2') {
printf("\n Specify type of element (1 for freestream, 2 for source, 3 for vortex): ");
scanf("%lf", &element1);
printf("\n Specify second type of element (1 for freestream, 2 for source, 3 for vortex): ");
scanf("%lf", &element2);
} else {
printf("\n ERROR Number of flow elements must be one or two, try again");
}
return (0);
}
答案 0 :(得分:2)
两个问题
如果你真的想检查'1'或'2'那么你必须分别输入49或50(假设是ASCII)