我想在我的循环中只有在超过声速之后才打印一条消息。
我在以下主题上找到了某人解释如何用布尔值来实现它,但我没有得到它。 这是我已经试过:做while循环:在循环内执行一次
boolean oneTime = (vitesse > 343)
if (oneTime)
System.out.println("Felix depasse la vitesse du son");
而且我的代码
while (h > 0)
{
while (speed < 343 && accel>0.5)
{
double s = surface/ masse;
double q = Math.exp(-s*(t-t0));
vitesse = (g/s)*(1-q) + v0*q;
hauteur = h0 - (g/s)*(t-t0)-((v0-(g/s))/s)*(1-q);
accel = g-s*vitesse; //formula to compute the fall of a body
if (h > 0)
{
if (speed > 343)
{
System.out.println("## Felix depasse la vitesse du son");
System.out.printf("%.0f, %.4f, %.4f, %.5f\n",t, hauteur, vitesse, accel);
}
else if (accel <0.5)
{
System.out.println("## Felix depasse la vitesse du son");
System.out.printf("%.0f, %.4f, %.4f, %.5f\n",t, hauteur, vitesse, accel);
}
else
System.out.printf("%.0f, %.4f, %.4f, %.5f\n",t, hauteur, vitesse, accel);
}
++t;
}
}
execute an instruction only one time in a do while loop in java
只需添加一个'break'当你需要退出你的循环。而且你有两个while循环,而不是在解决这个问题时做任何事情 –