0

I would like to print one message during my loop only after crossing the speed of sound.
I've found on the following topic someone explaining how to do it with a boolean but I didn't get it. This is what I've tried :

boolean oneTime = (vitesse > 343)
if (oneTime)
    System.out.println("Felix depasse la vitesse du son");

And my code

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

Community
  • 1
  • 1
Humble
  • 3
  • 3
  • Just add a `break` when you need to exit your loop. And you have two while loops, not any do while is going to fix that – OneCricketeer Apr 09 '17 at 14:59

3 Answers3

1

If you want the loop to continue but stop printing messages after the first time, you want something like this structure (pseudocode):

boolean oneTime = false;
while (...) {
    ...
    if (<some test for printing>) {
        if (!oneTime) {
            oneTime = true;
            <print your message>
        }
    }
    ...
    if (<some other test for printing>) {
        if (!oneTime) {
            oneTime = true;
            <print your message>
        }
    }
    ...
}

If you want the loop to exit when the message is printed, add !firstTime into the while loop condition (if you want the current loop iteration to complete) or use a break to end the loop immediately. If you go with break, then you don't need the oneTime flag at all. To break out of nested while loops, see this thread.

Community
  • 1
  • 1
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
0

Have a boolean initialized with false before the first loop and set it to true whenever you print out the felix message. Evaluate the boolean and print the felix message only if the criteria is met and the boolean is still false. You may call that boolean oneTime.

if (speed > 343 && !oneTime) {
    oneTime = true;
    System.out.println("## Felix depasse la vitesse du son");
} else if (accel < 0.5 && !oneTime) {
    oneTime = true;
    System.out.println("## Felix depasse la vitesse du son");
}
System.out.printf("%.0f, %.4f, %.4f, %.5f\n",t, hauteur, vitesse, accel);
Kriegel
  • 423
  • 5
  • 16
0

I noticed your second loop starts with while(speed < 343 && accel>0.5), said that, it is never going to make true your if statement as it is never going to reach 343 if(speed > 343) is always going to be false if you want it to run at least once you may use

    do{
    ...
    ...
    } while(speed < 343 && accel>0.5);
Edwin Barahona
  • 189
  • 1
  • 3
  • 13