I need to remove break; statements in my code and I’ve run out of ideas how to do it. Can you please help me?
Scanner sc = new Scanner(System.in);
double g = 1.62, v0, deg = 45;
double inRadians = Math.toRadians(deg);
double t, x, y, x0 = 0, y0 = 0;
boolean hitTarget = false;
boolean hitObstacle = false;
System.out.println("211RDB121 Pauls Daniels Meija 11.grupa");
System.out.print("v0=");
if (sc.hasNextDouble())
v0 = sc.nextDouble();
else {
System.out.println("input-output error");
sc.close();
return;
}
sc.close();
System.out.println("result:");
System.out.println("t t x t y");
t = 0.15;
do {
x = v0 * t * Math.cos(inRadians) + x0;
y = v0 * t * Math.sin(inRadians) - (g * t * t) / 2 + y0;
System.out.printf("%3.2ft%7.3ft%7.3fn", t, x, y);
if (x >= 17 && x <= 20 && y <= 3 && y >= 0) {
hitTarget = true;
break;
}
else if (x >= 7 && x <= 10 && y <= 4 && y >= 0) {
hitObstacle = true;
break;
}
t += 0.1;
} while (y >= 0);
if (hitObstacle) {
System.out.println("hit the obstacle!");
}
else if (hitTarget)
System.out.println("the target was destroyed");
else
System.out.println("shot off the target");
}```
Advertisement
Answer
boolean run = true;
do {
x = v0 * t * Math.cos(inRadians) + x0;
y = v0 * t * Math.sin(inRadians) - (g * t * t) / 2 + y0;
System.out.printf("%3.2ft%7.3ft%7.3fn", t, x, y);
hitTarget = x >= 17 && x <= 20 && y <= 3 && y >= 0;
hitObstacle = !hitTarget && x >= 7 && x <= 10 && y <= 4 && y >= 0;
if (!hitTarget && !hitObstacle) {
t += 0.1;
}
run = y >= 0 && !hitTarget && !hitObstacle;
} while (run);