You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C语言switch语句输入多字符时重复输出错误提示问题求助

解决输入多字符时default分支重复触发的问题

你的问题根源在于scanf(" %c", &PI1)的行为逻辑:这个格式串会跳过空白字符,但每次仅读取一个非空白字符。当输入"No thanks"这类多字符内容时,程序会将每个字符依次取出处理,每个无效字符都会触发一次default分支的提示,因此才会出现8次重复输出的情况。

下面提供两种实用的解决方法:

方法一:读取整行输入,仅处理第一个有效字符

使用fgets读取完整的一行输入,然后提取其中第一个非空白字符进行判断,这样无论输入多长,只会触发一次判断逻辑,剩余字符直接忽略。

修改后的代码示例:

#include <ctype.h> // 需要包含此头文件以使用isspace函数

// ... 原有变量定义 ...

while (Scene1 == 0) {
    char input[100];
    // 读取整行输入,最多读取99个字符(预留位置存储结束符)
    if (fgets(input, sizeof(input), stdin) == NULL) {
        // 处理读取失败或EOF的情况
        break;
    }

    char PI1 = '\0';
    // 遍历输入内容,找到第一个非空白字符
    for (int i = 0; input[i] != '\0'; i++) {
        if (!isspace((unsigned char)input[i])) {
            PI1 = input[i];
            break;
        }
    }

    if (PI1 == '\0') {
        // 输入全为空白字符,重新提示
        printf("That's not an option. Try again.\n");
        continue;
    }

    switch(PI1) {
        case 'A':
            if (Scene1War == 1) {
                printf("The Goblin is cowed, and agrees to let you through if you spare his life.");
                Scene1++;
            } else {
                printf("The Goblin laughs and states that if he wanted anything he'd take it from your corpse.\n");
            }
            break;
        case 'B':
            printf("You make a terrified expression and stare intently into the doorway behind the Goblin. \nHe is confused and turns around for a second.\n");
            Scene1Dis++;
            break;
        case 'C':
            if (Scene1Dis == 1) {
                printf("You catch the Goblin by surprise and manage to gain the upper hand. He does manage to get away alive, however.");
                Scene1++;
            } else {
                printf("You and the Goblin fight viciously. In the end he is killed and you have lost 1 HP.");
                GobLife++;
                HP--;
                Scene1++;
            }
            break;
        case 'D':
            if (Class == 2) {
                printf("You stand menacingly and tell the Goblin you mean business.\n");
                Scene1War++;
            } else if (Class == 3) {
                if (Scene1Dis == 1) {
                    printf("You successfully sneak by the Goblin.");
                    Scene1++;
                } else {
                    printf("The Goblin notices you trying to sneak past, and stops you.\n");
                }
            } else {
                printf("That's not an option. Try again.\n");
            }
            break;
        default:
            printf("That's not an option. Try again.\n");
            break;
    }
}

方法二:读取单个字符后清空输入缓冲区

如果希望继续使用scanf,可以在每次读取字符后,清空输入缓冲区中剩余的所有字符(直到换行符或EOF),避免下次循环读取到之前输入的残留字符。

修改后的代码示例:

while (Scene1 == 0) {
    scanf(" %c", &PI1);
    
    // 清空输入缓冲区:读取所有剩余字符直到换行或EOF
    int c;
    while ((c = getchar()) != '\n' && c != EOF);

    switch(PI1) {
        // 保留原有所有case分支
        case 'A':
            if (Scene1War == 1) {
                printf("The Goblin is cowed, and agrees to let you through if you spare his life.");
                Scene1++;
            } else {
                printf("The Goblin laughs and states that if he wanted anything he'd take it from your corpse.\n");
            }
            break;
        case 'B':
            printf("You make a terrified expression and stare intently into the doorway behind the Goblin. \nHe is confused and turns around for a second.\n");
            Scene1Dis++;
            break;
        case 'C':
            if (Scene1Dis == 1) {
                printf("You catch the Goblin by surprise and manage to gain the upper hand. He does manage to get away alive, however.");
                Scene1++;
            } else {
                printf("You and the Goblin fight viciously. In the end he is killed and you have lost 1 HP.");
                GobLife++;
                HP--;
                Scene1++;
            }
            break;
        case 'D':
            if (Class == 2) {
                printf("You stand menacingly and tell the Goblin you mean business.\n");
                Scene1War++;
            } else if (Class == 3) {
                if (Scene1Dis == 1) {
                    printf("You successfully sneak by the Goblin.");
                    Scene1++;
                } else {
                    printf("The Goblin notices you trying to sneak past, and stops you.\n");
                }
            } else {
                printf("That's not an option. Try again.\n");
            }
            break;
        default:
            printf("That's not an option. Try again.\n");
            break;
    }
}

关于你提到的改成字符串导致switch失效的问题:C语言的switch仅支持整数类型(char本质属于整数类型),因此无法直接使用字符串,但可以像方法一那样提取字符串的第一个字符,再用该字符进行switch判断,这样既能处理整行输入,又能正常使用switch逻辑。

内容的提问来源于stack exchange,提问作者BookwormBoy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 17:18:11