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

C语言if语句失效求助:宇航员适配程序无法执行判断逻辑

问题排查与修复方案

核心问题分析

1. 条件分支覆盖不全

原代码的else仅对应最外层的体重判断分支,仅当体重不符合范围时才会输出提示。如果体重符合但年龄不达标,或者年龄达标但吸烟状态不符合要求,程序不会输出任何信息,直接执行return(0)退出,导致用户误以为程序未进入判断逻辑。比如测试用例中输入年龄19,小于设置的最小年龄20,此时程序跳过所有内层if,也不会触发else,因此无输出。

2. 吸烟状态判断逻辑错误

代码中if (smoking != 'S' || smoking != 's')的逻辑存在根本性错误:任意输入的字符都满足该条件(因为一个字符不可能同时是'S'和's'),这会导致即使输入'S'或's',也会进入“符合要求”的分支,完全违背需求。正确逻辑应该是判断字符既不是大写S也不是小写s,需用&&替代||。

3. 拼写错误

提示文本中的astronout为拼写错误,正确应为astronaut。

修复后的代码

方案一:补全分支判断

#include <stdio.h>

int
main(void)
{
    int opt_min;
    int opt_max;
    int age_min;
    int age_max;
    int age;
    double weight;
    char smoking;

    printf("Enter the minimum weight an astronaut can be>");
    scanf("%d", &opt_min);

    printf("Enter the maximum weight an astronaut can be>");
    scanf("%d", &opt_max);

    printf("Enter the minimum age an astronaut can be>");
    scanf("%d", &age_min);

    printf("Enter the maximum age an astronaut can be>");
    scanf("%d", &age_max);

    printf("Enter your weight>");
    scanf("%lf", &weight);

    printf("Enter your age>");
    scanf("%d", &age);

    printf("Enter the state of smoking\n(if a smoker press 'S' if not press another letter and press return)>");
    scanf(" %c", &smoking);

    if (weight >= opt_min && weight <= opt_max) {
        if (age >= age_min && age <= age_max) {
            if (smoking != 'S' && smoking != 's') {
                printf("You can be an astronaut!");
            } else {
                printf("Sorry! Your qualities are not enough to be an astronaut.");
            }
        } else {
            printf("Sorry! Your qualities are not enough to be an astronaut.");
        }
    } else {
        printf("Sorry! Your qualities are not enough to be an astronaut.");
    }

    return (0);
}

方案二:用状态变量简化逻辑

#include <stdio.h>

int
main(void)
{
    int opt_min;
    int opt_max;
    int age_min;
    int age_max;
    int age;
    double weight;
    char smoking;
    int is_eligible = 1;

    printf("Enter the minimum weight an astronaut can be>");
    scanf("%d", &opt_min);

    printf("Enter the maximum weight an astronaut can be>");
    scanf("%d", &opt_max);

    printf("Enter the minimum age an astronaut can be>");
    scanf("%d", &age_min);

    printf("Enter the maximum age an astronaut can be>");
    scanf("%d", &age_max);

    printf("Enter your weight>");
    scanf("%lf", &weight);

    printf("Enter your age>");
    scanf("%d", &age);

    printf("Enter the state of smoking\n(if a smoker press 'S' if not press another letter and press return)>");
    scanf(" %c", &smoking);

    if (!(weight >= opt_min && weight <= opt_max)) {
        is_eligible = 0;
    } else if (!(age >= age_min && age <= age_max)) {
        is_eligible = 0;
    } else if (smoking == 'S' || smoking == 's') {
        is_eligible = 0;
    }

    printf(is_eligible ? "You can be an astronaut!" : "Sorry! Your qualities are not enough to be an astronaut.");

    return (0);
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 03:15:31