Frenchie - 2 months ago 6
Java Question

# How to print a line BEFORE the condition is met

It's probably very simple, but I can't find it...

I am writing a program that calculates the parameters of the fall of a skydiver and displays results (altitude, speed and acceleration) second by second until he reaches the ground.

At one point (when the altitude is smaller than 2500m) he will open his parachute. And the program should print (only once) "##Felix opens his parachute" just BEFORE he reaches 2500m.

So the result sholud be:
(time, altitude, speed, acceleration)
131, 2698.0264, 377.5607, 0.37098
"##Felix opens his parachute"
132, 2320.2818, 377.9270, 0.36182
133, 1991.2751, 284.9225, -79.22827...

But I can't find a way to do it. My program prints the line AFTER the condition is met:
(time, altitude, speed, acceleration)
131, 2698.0264, 377.5607, 0.37098
132, 2320.2818, 377.9270, 0.36182
"##Felix opens his parachute"
133, 1991.2751, 284.9225, -79.22827..

Here's the code:

``````import java.util.Scanner;
``````

class Parachutiste {

``````public static void main(String[] args) {

Scanner clavier = new Scanner(System.in);

double masse = 80.0;
do {
System.out.print("masse du parachutiste (>= 40) ? ");
masse = clavier.nextDouble();
} while (masse < 40.0);

double h0 = 39000.0;
do {
System.out.print("hauteur de depart du parachutiste (>= 250) ? ");
h0 = clavier.nextDouble();
} while (h0 < 250.0);

/*******************************************
* Completez le programme a partir d'ici.
*******************************************/
final double g = 9.81;
double v0 = 0;
double t0 = 0;

double sourface = 2.00;
double vitesse = v0;
double hauteur = h0;
double accel = g;
double t = t0;

boolean printed = false;

System.out.printf("%.0f, %.4f, %.4f, %.5f\n", t, hauteur, vitesse, accel);

while (hauteur > 0) {
++t;
double s = sourface / 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;

if (hauteur > 0) {

System.out.printf("%.0f, %.4f, %.4f, %.5f\n", t, hauteur, vitesse, accel);

if (hauteur < 2500) {
sourface = 25.0;
t0 = t;
v0 = vitesse;
h0 = hauteur;
}

**if ((hauteur <= 2500) && (printed == false))  {
System.out.println("## Felix ouvre son parachute");
printed = true;**
}

}
}

/*******************************************
* Ne rien modifier apres cette ligne.
*******************************************/
clavier.close();
}
``````

}

Why not move the condition that prints the "Felix opens his parachute" line before the code that prints the current altitude ?

``````while (hauteur > 0) {
++t;
double s = sourface / 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;

if (hauteur > 0) {

if (hauteur < 2500) {
sourface = 25.0;
t0 = t;
v0 = vitesse;
h0 = hauteur;
}

if ((hauteur <= 2500) && !printed)  {
System.out.println("## Felix ouvre son parachute");
printed = true;
}

System.out.printf("%.0f, %.4f, %.4f, %.5f\n", t, hauteur, vitesse, accel);

}
}
``````