c - 减法运算不起作用

标签 c windows

#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/

相关文章:

c - AVR C : Int to hexadecimal address

ruby-on-rails - 在端口 80 上的 Windows 中启动 Rails 服务器?

java - Win7/10 上 Tahoma 的不同字体版本?

windows - Powershell 获取 Windows 中显示的区分大小写的路径

windows - 什么是 Windows Fabric 以及如何在其中托管服务?

C - 将字符串分割成多个部分

c - 当依赖 DLL 丢失时,尝试使用 LoadLibrary 在 Windows 上加载 DLL

objective-c - memcpy 导致 EXC_BAD_ACCESS

C : error: expected expression before âSRT_ControleComplet_Sâ

c# - 法国应该使用哪个时区来支持新旧夏令时调整?