c for循环,而不是if语句 [英] c for loop instead of an if statement

查看:71
本文介绍了c for循环,而不是if语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    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的值.

so basically I am making a program to get data values for a bouncing ball and I need to know the bounce heights which is calculated using COR (coefficient of restitution squared) * drop height. I need it to be able to loop through the program so after the first bounce height is calculated that value is then used in the equation to get the next one and so on. I am unsure on how to do this I started with an IF statement but that will only do as many calculations as i program in. Any help with this would be amazing thanks in advance. PS the values for COR and height are asked for from the user early in the program.

推荐答案

您应使用 while 循环并设置最小高度以停止循环.

You should use a while loop and set a minimum heigth that stops the loop.

类似的东西:

#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

This will keep printing the heigth after each bounce until the heigth get below MINIMUM_HEIGTH

这篇关于c for循环,而不是if语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆