While 或 Switch 仅检测 C 中的小写字母而不检测大写字母

While or Switch only detect lower-case and not upper-case in C

我想让用户写一个字母来选择,问题是它只检测小写字母而不检测大写字母,你能帮我吗?

#include <stdio.h>
#include <ctype.h>

int main ()
{
    char choice;
    
    printf("Will you choose A, or B?\n>");
    
    do {
        scanf(" %c", &choice);
    } while (choice != 'a' && 'A' && choice != 'b' && 'B');

    switch (choice) {
        case 'A':
        case 'a':
            printf("The First Letter of the Alphabet\n");
            break;
        case 'B':
        case 'b':
            printf("The Second Letter of the Alphabet\n");
            break;
    }

    system("pause");
    return 0;
}

choice != 'a' && 'A' && choice != 'b' && 'B'

'A''B' 只是被解释为“真”——表达式需要是

choice != 'a' && choice != 'A' && choice != 'b' && choice != 'B'

更好的替代方法可能是将开关移到循环中,确保循环退出条件和开关一致。