C 中的嵌套 if 语句 - 为什么它不评估最后一个 else if?

Nested if statement in C - why doesn't it evaluate the last else if?

当您分配给 choice3 时,以下代码不会执行最后一个 else if 语句。

#include<stdio.h>
#include<stdlib.h>

int main() {
    puts("Specify with a number what is that you want to do.");
    puts("1. Restore wallet from seed.");
    puts("2. Generate a view only wallet.");
    puts("3. Get guidance on the usage from within monero-wallet-cli.");

    unsigned char choice;
    choice = getchar(); 
 
    if ( choice == '1' ) {
        system("nice -19 ~/monero-x86_64-linux-gnu-v0.17.2.0/monero-wallet-cli --testnet --restore-deterministic-wallet"); 
        exit(0);
    }
    else if ( choice == '2' ) {
        system("nice -19 ~/monero-x86_64-linux-gnu-v0.17.2.0/monero-wallet-cli --testnet --generate-from-view-key wallet-view-only");
        exit(0);
    }
    else if ( choice == '3' ) {    
        puts("Specify with a number what is that you want to do.");
        puts("1. Get guidance in my addresses and UTXOs");
        puts("2. Pay");
        puts("3. Get guidance on mining.");
    
        unsigned char choicetwo = getchar();
        if ( choicetwo == '1' ) {      
            printf("Use 3address all3 to get all your addresses that have any balance, or that you have generated at this session.");
            printf("Use 3balance3 to get your balance");
            printf("Use 3show_transfers3 to get ");
            printf("Use 3show_transfers3 out to get ");
            printf("Use 3show_transfers in3 to get your balance");
        }
    }

    return 0;   
}

当我输入 3:

时,我得到以下输出
Specify with a number what is that you want to do.
1. Restore wallet from seed.
2. Generate a view only wallet.
3. Get guidance on the usage from within monero-wallet-cli.
3
Specify with a number what is that you want to do.
1. Get guidance in my addresses and UTXOs
2. Pay
3. Get guidance on mining.

我真的被屏蔽了,缺少了一些东西,我不知道为什么它没有第二次从用户那里获取输入。

当您第一次输入“3”时,您实际上输入了两个字符:字符 '3' 和一个换行符。第一个 getchar 函数从输入流中读取“3”,第二个函数读取换行符。

接受第一个输入后,您需要循环调用 getchar,直到读取换行符以清除输入缓冲区。

choice = getchar();
while (getchar() != '\n');