I'm completing an assignment give to me by my professor, and it states that I can't use break in my code, but without break the code doesn't stop when it needs to. I tried a couple of different options that I found on the internet, but nothing worked. This is my code:
while (x >= 0 && x <= 11 && y >= 0 || x >= 11 && x <= 22 && y >= -7) {
x = v0 * t*Math.cos(Math.toRadians(a));
y = v0 * t*Math.sin(Math.toRadians(a)) - g * (t*t) / 2;
System.out.printf("%3.2f\t%7.3f\t%7.3f\n", t, x, y);
if (x <= 20 && x >= 17 && y <= -2 && y >= -7) {
hitTarget = true;
break;
}
t += 0.05;
}
Put !hitTarget
in while
. Since you're setting hitTarget = true
when that condition gets satisfied, the loop will stop
while (!hitTarget && x >= 0 && x <= 11 && y >= 0 || x >= 11 && x <= 22 && y >= -7) {
//..
}
; Logical complement operator; inverts the value of a boolean
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.