[英]Java for loop gets skipped - my mistake?
I have already some c# knowledge but I'm really confused by such an easy thing, but maybe the problem is deeper than I expect.我已经有一些 c# 知识,但我真的被这么简单的事情搞糊涂了,但也许问题比我预期的要深。 I imported a "little game" for learning java and wrote that code:
我导入了一个用于学习 Java 的“小游戏”并编写了该代码:
import de.ur.mi.bouncer.apps.BouncerApp;
public class DasErsteHindernis extends BouncerApp {
@Override
public void bounce() {
loadMap("Obstacles");
runtillwall();
climbupwards();
}
private void runtillwall(){
while(bouncer.canMoveForward() == true){
bouncer.move();
}
}
private void climbupwards(){
bouncer.turnLeft();
bouncer.move();
for(int i = 0; i==2; i++){
bouncer.turnLeft();
}
}
But the for loop gets skipped -> the bouncer doesn't turn left.但是 for 循环被跳过 - > 保镖不会左转。 What did I do wrong?
我做错了什么?
The middle part of a for loop is a condition. for 循环的中间部分是一个条件。 Yours is saying while i is equal to 2, do this.
你的意思是当 i 等于 2 时,这样做。 It never gets to two, so it never executes.
它永远不会达到两个,所以它永远不会执行。 You should be using the less-than or less-than-or-equal-to sign.
您应该使用小于或小于或等于符号。 (< <=)
(< <=)
for(int i = 0; i==2; i++){
Here you are initializing i
with 0,so first when loop starts i
is 0 ,then middle part which is condition in your loop that should be true for loop to start.In your case,you are saying if i
is equal to 2 then execute the instruction.It fails and your loop body is not executed single time.在这里你用 0 初始化
i
,所以首先当循环开始时i
是 0 ,然后中间部分是循环中的条件,循环开始时应该为真。在你的情况下,你是说如果i
等于 2 然后执行指令。它失败了,你的循环体不会被执行一次。
it seems you need看来你需要
for(int i = 0; i<=2; i++){
This one will execute loop body 3 times.这将执行循环体 3 次。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.