简体   繁体   English

如何更改我的代码,这样我就不必使用“break”?

[英]How do I change my code, so that I don't have to use “break”?

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.我正在完成我的教授给我的作业,它指出我不能在我的代码中使用 break,但是如果没有break ,代码不会在需要时停止。 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 .!hitTarget放入while中。 Since you're setting hitTarget = true when that condition gets satisfied, the loop will stop由于您在满足该条件时设置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反转 boolean 的值

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何更改 eclipse 以便我不必手动导入 - How do I change eclipse so I don't have to manually import 在第 11 行,我该怎么做才能不需要使用那么多空格 - In line 11 how do i make it so that i don't need to use so many spaces 如何修复我的数组,以免出现 IndexOutOfBoundsException? - How can I fix my array so I don't have an IndexOutOfBoundsException? 有没有办法做到这一点,所以我没有 64 个 if 语句? - Is there a way to do this so I don't have 64 if statements? 如何简化MouseListener,以便没有所有这些未使用的方法? - How do I simplify MouseListener so that I don't have all these unused methods? 如何绘制Java2D仿真的一部分,而该部分不会更改为图像/缓冲区,因此不必每次都重新绘制其图元? - How can I draw a portion of my Java2D simulation that doesn't change to an image/buffer so I don't have to redraw it's primitives each time? 我不知道如何更改我的程序以便我可以看到十个最大的文件 - I don´t know how to change my program so that i can see the ten largest files 如何设置一个<resource>在 Tomcat 7 中,这样我就不需要在代码中使用“java:/comp/env”了吗?</resource> - How to set up a <Resource> in Tomcat 7 so that I don't need to use “java:/comp/env” in the code? 我的代码中有一个nullPointerException,我不知道如何解决 - I have a nullPointerException in my code that I don't know how to resolve 我如何更改我的代码,以便如果有第二个输入,它将添加到我的直方图上? - How do i change my code so that if theres a second input it will add onto my histogram?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM