【问题标题】:c for loop instead of an if statementc for 循环而不是 if 语句
【发布时间】:2021-04-10 12:18:02
【问题描述】:
    distance = (u * t + (0.5 * (a * t * t)));
    printf("distance is ");
    printf("%0.5f\n", distance);
    printf("\n");


        if (distance >= height)
        {
            bounce = ((COR*COR)*height);
            printf("The bounce is ");
            printf("%0.5f\n", bounce);
        
            
            bounce_height = bounce;
            
            bounce = ((COR*COR)*bounce_height);
            printf("The bounce is ");
            printf("%0.5f", bounce);
            break;
            
        }

所以基本上我正在制作一个程序来获取弹跳球的数据值,我需要知道使用 COR(恢复系数平方)* 落差计算的弹跳高度。我需要它能够循环通过程序,因此在计算出第一个弹跳高度后,该值将用于等式中以获取下一个弹跳高度,依此类推。我不确定如何做到这一点,我从一个 IF 语句开始,但这只会做与我编程一样多的计算。在此先感谢您的任何帮助。 PS COR 和 height 的值是在程序早期向用户询问的。

【问题讨论】:

  • 类似于while (bounce_height > 0) { /* ... print ... calculate new bounce_height ... */ }
  • 提示:(u + 0.5*a*t) * t 在数值上比u * t + (0.5 * (a * t * t)) 更稳定

标签: c for-loop if-statement


【解决方案1】:

您应该使用while 循环并设置停止循环的最小高度。

类似:

#define MINIMUM_HEIGTH 0.1

float current_heigth = 2.0;
while (current_heigth > MINIMUM_HEIGTH)
{
    printf("Current heigth is %f\n", current_heigth);
    current_heigth = calculate_new_heigth_after_bounce...;
}

这将在每次反弹后继续打印高度,直到高度低于 MINIMUM_HEIGTH

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-26
    • 2013-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多