如何更改我的代码,以便我不必使用 "break"?

How do I change my code, so that I don't have to use "break"?

我正在完成教授给我的作业,它指出我不能在我的代码中使用 break,但没有 break 代码不会在需要时停止。我尝试了几个在互联网上找到的不同选项,但没有任何效果。这是我的代码:

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;
    }

!hitTarget放入while。由于您正在设置 hitTarget = true 当该条件得到满足时,循环将停止

while (!hitTarget && x >= 0 && x <= 11 && y >= 0 || x >= 11 && x <= 22 && y >= -7) {
    //..
}

Unary Operators

! Logical complement operator; inverts the value of a boolean