[英]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) {
//..
}
;
; Logical complement operator;
逻辑补码运算符; inverts the value of a boolean
反转 boolean 的值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.