gpt4 book ai didi

c - 我的 if 和 else if 同时运行

转载 作者:行者123 更新时间:2023-11-30 21:31:25 25 4
gpt4 key购买 nike

我有一个程序测量从火箭到月球的距离,因此当距离 >250 时,仅运行 if。然而,当 else 运行并且距离 <=250 时,if 继续运行,而 else 运行。如果有人能帮我解决它,我将非常感激。

do
{
if (distance >250)
{
time += 1;
y_pos = initial_y_pos - (vel_y * time)/2;
x_pos = initial_x_pos + (vel_x * time)/2;
//Sets the new x and y position when time is flowing so the rocket can move
GFX_DrawLineTo(x_pos, y_pos, 3);
GFX_UpdateDisplay();
distance = sqrt(pow((y_pos-(y+312)),2)+(pow((x_pos-(x+440)),2)));
mars_dist = sqrt(pow((y_pos-150),2)+pow((x_pos-1150),2));
//distance calculation from the rocket to the moon. Needed for sphere of influence
printf("%f\n",distance);
}

else if (distance <=250)
{
y_pos = initial_y_pos - ((vel_y * time) - ((gravity * time)/2));
x_pos = initial_x_pos + ((vel_x * time) - ((gravity * time)/2));
GFX_DrawLineTo(x_pos, y_pos, 3);
GFX_UpdateDisplay();
distance = sqrt(pow((y_pos-(y+312)),2)+(pow((x_pos-(x+440)),2)));
mars_dist = sqrt(pow((y_pos-150),2)+pow((x_pos-1150),2));
//printf("%f\n",distance);
printf("%f\n", x_pos);
}


if (distance <= 50)
//If the rocket either hits moon or mars, this is responsible for recognising that
{
printf("Unlucky! Your rocket crashed into the moon!");
return 0;
}

}
//while (distance > 250);
while ((0 <= x_pos && x_pos <= 1280) || (0 <= y_pos && y_pos <= 1024));

最佳答案

when the else runs and the distance <=250, the if continues to run while the else runs

好吧,你的意思是distance <=250然后下一个同时转distance>250

当计算的速度/覆盖距离太高并且火箭在延迟时间后穿过火星到达另一侧远超过250(或者火箭进入火星内部?)时,就会发生这种情况,这意味着你必须改变

    y_pos = initial_y_pos - ((vel_y * time) - ((gravity * time)/2));
x_pos = initial_x_pos + ((vel_x * time) - ((gravity * time)/2));
...
distance = sqrt(pow((y_pos-(y+312)),2)+(pow((x_pos-(x+440)),2)));
mars_dist = sqrt(pow((y_pos-150),2)+pow((x_pos-1150),2));

如果速度/覆盖距离太高,则降低速度/覆盖距离。或者速度/覆盖距离可能是正确的,但火箭在不到时间的时间内到达表面或火星,因此新位置是不正确且不可能的,因为假设火箭可以穿过火星表面,你必须检测到这种情况。

关于c - 我的 if 和 else if 同时运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55101427/

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