#include <stdio.h>
#include <stdlib.h>
int main(){
char letter;
float n1, n2, sum;
printf("What operation do you want to use \n\nA)Addidtion\nB)Subtraction\nC)Multiplacation\nD)Division\n");
scanf("%c", &letter);
printf("Please enter a number: ");
scanf("%f", &n1);
printf("Please enter a second number: ");
scanf("%f", &n2);
if(letter == 'A' ||'a'){
sum = n1 + n2;
printf("The sum of %.2f plus %.2f is %.2f", n1, n2, sum);
}else if(letter == 'b' || 'B'){
sum = n1 - n2;
printf("The sum of %.2f minus %.2f is %.2f", n1, n2, sum);
}
return 0;
}
减法不起作用你能帮我解决这个问题吗?我不能,当我运行程序时,我键入 b,然后键入 12,然后键入 2,它给了我 14?应该是 10
最佳答案
if(letter == 'A' ||'a')
没有做你想做的事。
if(letter == 'A' || letter == 'a')
就是你想要的。
||
操作将其操作数评估为真值,如果其中一个操作数评估为 true
,则评估为 1
。无论 letter == 'A'
是否为真,至少 'a'
- 即 != 0
- “带来了真相".
关于c - 减法运算不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20549997/