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

如何让C语言循环未被break时才执行后续速度判断代码

解决C语言循环正常结束后才执行后续代码的问题

针对需求——仅当while循环因height <= 0正常结束,而非因非法输入触发break跳出时,才执行速度判断逻辑,以下是三种实用实现方法:

方法一:使用标志变量追踪循环退出原因

定义一个标志变量记录循环是否正常结束,触发break时修改标志值,循环结束后根据标志决定是否执行速度判断:

#include <stdio.h>

int main() {
    int loop_finished_normally = 1; // 初始化标志,默认循环正常结束
    // 注意:原代码中height、throttle、K、G等变量需提前初始化,此处保留原结构仅添加关键修改
    
    while (height > 0) {
        if (throttle >= 0 && throttle <= 100) {
            printf("%d    %.1f  %.1f   %.1f  ", time, height, velocity, fuel);
            scanf("%d", &throttle);
            height = heightTemp + velocityTemp - (throttle * K - G) / 2;
            velocity = velocityTemp + (throttle * K - G);
            fuel = fuelTemp - throttle;
            time = time + 1;
            heightTemp = height;
            velocityTemp = velocity;
            fuelTemp = fuel;   
        }
        else {
            printf("Please choose a number between 0 and 100! \n");
            loop_finished_normally = 0; // 标记循环非正常结束
            break;
        }
    }
   
    // 仅当循环正常结束时执行速度判断
    if (loop_finished_normally) {
        if (velocity >= -2.0) {
            printf("You landed successfully: ");
        } 
        else {
            printf("Failed! You crashed");
        }
    }

    return 0;
}

方法二:直接检查循环退出的条件

循环正常结束的唯一原因是height <= 0,因此循环结束后判断该条件是否成立即可:

#include <stdio.h>

int main() {
    // 变量初始化略
    
    while (height > 0) {
        if (throttle >= 0 && throttle <= 100) {
            printf("%d    %.1f  %.1f   %.1f  ", time, height, velocity, fuel);
            scanf("%d", &throttle);
            height = heightTemp + velocityTemp - (throttle * K - G) / 2;
            velocity = velocityTemp + (throttle * K - G);
            fuel = fuelTemp - throttle;
            time = time + 1;
            heightTemp = height;
            velocityTemp = velocity;
            fuelTemp = fuel;   
        }
        else {
            printf("Please choose a number between 0 and 100! \n");
            break;
        }
    }
   
    // 仅当height <= 0(循环正常结束)时执行速度判断
    if (height <= 0) {
        if (velocity >= -2.0) {
            printf("You landed successfully: ");
        } 
        else {
            printf("Failed! You crashed");
        }
    }

    return 0;
}

方法三:将速度判断逻辑嵌入循环正常结束分支

在循环内部提前检测height是否即将变为<=0,满足条件时直接执行速度判断并退出循环:

#include <stdio.h>

int main() {
    // 变量初始化略
    
    while (height > 0) {
        if (throttle >= 0 && throttle <= 100) {
            printf("%d    %.1f  %.1f   %.1f  ", time, height, velocity, fuel);
            scanf("%d", &throttle);
            height = heightTemp + velocityTemp - (throttle * K - G) / 2;
            velocity = velocityTemp + (throttle * K - G);
            fuel = fuelTemp - throttle;
            time = time + 1;
            heightTemp = height;
            velocityTemp = velocity;
            fuelTemp = fuel;

            // 检测是否即将正常退出循环,执行速度判断
            if (height <= 0) {
                if (velocity >= -2.0) {
                    printf("You landed successfully: ");
                } 
                else {
                    printf("Failed! You crashed");
                }
                break;
            }
        }
        else {
            printf("Please choose a number between 0 and 100! \n");
            break;
        }
    }

    return 0;
}

注意:原代码中height、throttle、K等变量未初始化,实际运行时需补充初始化逻辑,否则会出现未定义行为。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 10:03:33