gpt4 book ai didi

模拟发射物体抛物线轨迹的 C 代码提前终止

转载 作者:太空宇宙 更新时间:2023-11-04 06:33:40 25 4
gpt4 key购买 nike

该程序模拟以固定的垂直和水平速度以一定角度发射的物体的抛物线轨迹。它以终端控制台中显示的坐标输出数据。

但是,程序只输出到第二行就终止了,所以一定是代码某处出错了。我无法识别错误,所以我请求帮助!

 #include <stdio.h>
#include <stdlib.h>

int main(void) {
float lvelox;
float lveloy;
float xcord;
float ycord;
int stepcount;
int step = 0;

/* Initializing velocity */
{
printf("Enter the initial h velocity of the ball:\n");
scanf("%f", &lvelox);
printf("Enter the initial v velocity of the ball:\n");
scanf("%f", &lveloy);
}

/* Obtain number of steps */
{
printf("Enter the number of steps wanted:\n");
scanf("%d", &stepcount);
}

/* formula for calculating initial position */
if ( step == 0 )
{
xcord = 0;
ycord = 0;
step = step + 1;
printf("\n");
printf("xcord, ycord, step\n");
printf("\n");
printf("%f, ", xcord);
printf("%f, ", ycord);
printf("%d\n", step);
}

/* Loop method */
if ( step < stepcount )
{
lveloy = lveloy - 9.81;
xcord = xcord + lvelox;
ycord = ycord + lveloy;
step = step + 1;
printf("%f, ", xcord);
printf("%f, ", ycord);
printf("%d\n", step);

if ( ycord < 0 )
{
lveloy = (lveloy * -1);
lveloy = lveloy - 9.81;
xcord = xcord + lvelox;
ycord = ycord + lveloy;
step = step + 1;
printf("%f, ", xcord);
printf("%f, ", ycord);
printf("%d\n", step);
}
}

if (step >= stepcount)
{
return 0;
}

}

最佳答案

我想你想要一个循环而不是 if,在你的代码中:

 if ( step < stepcount ) 

应该是:

 while ( step < stepcount )

关于模拟发射物体抛物线轨迹的 C 代码提前终止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17638054/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com