简体   繁体   English

你能帮我解决我的 for 循环吗? 它没有转动正确的度数

[英]Can you help me with my for loop? it isn't turning the right amount of degrees

public void turnLeft(double degrees) {
    for (int i = 0; i < degrees; i++) {
        this.left(degrees);
    }
}

This code helps my turtle turn how many ever degrees I want it to, but when I put in like 180 degrees it only turns 90.这段代码可以帮助我的乌龟转动我想要的度数,但是当我放入 180 度时,它只会转动 90 度。

I can only guess that your implementation of the left method rotates turtle on degrees number, specified with method parameter.我只能猜测你的left方法的实现会根据方法参数指定的度数旋转乌龟。

In your case (calling of the turnLeft(180) ) the loop will do the following:在您的情况下(调用turnLeft(180) )循环将执行以下操作:

this.left(1); this.left(2); ... this.left(180);

so... it very similar with calling the turnLeft method with number of degrees equals 1+2+3+...+180 it is 16110所以...它与调用turnLeft方法非常相似,度数等于1+2+3+...+180它是16110

So in this case your turtle makes 16110 div 360 = 44 whole cycles and then rotate on 16110 mod 360 = 270 degrees.因此,在这种情况下,您的乌龟使16110 div 360 = 44完整周期,然后旋转16110 mod 360 = 270度。 So it will be in 90 degrees from your initial state.所以它将与您的初始状态成90度。 But it is only my guess what could be happen.但这只是我的猜测可能会发生什么。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM