This site requires JavaScript, please enable it in your browser!
Greenfoot back
HeyHey
HeyHey wrote ...

2013/4/3

while, for Loops run 4 times?

HeyHey HeyHey

2013/4/3

#
Hy Guys i tried to use a while and a for loop. System.out.println showed me that the loops sometimes run 4 times instead of one time. Whats wrong with it?
int meineZahl = 3;
    public void act() 
    {

        
        while (meineZahl < 10000)
        {
            System.out.println(meineZahl);
            meineZahl = meineZahl * meineZahl;
        }

    }    
Greetings HeyHey
danpost danpost

2013/4/3

#
For each loop through the 'while' loop, the value of meineZahl will be (1) 3; (2) 9; (3) 27; (4) 81; (5) 243; (6) 729; (7) 2187; and (8) 6561. On the ninth time the value will be '19683' and will not execute the code within the loop. This will only happen for the first act cycle. On future act cycles it should not execute the code within the loop at all. I do not know where you got 4 and 1 for the loop counts, but the code appears to show 8 (once) and never again (unless the value of 'meineZahl' is changes somewhere outside the given code).
HeyHey HeyHey

2013/4/3

#
sometimes if loops will run a few times too Output: 10 10 10 10 10 10 10 10 The Code: (maxWaypoints has the value 8)
 tokenPlayer1Step=9;
           if(tokenPlayer1Step > maxWaypoints)
           {
               tokenPlayer1Step+=1;
               System.out.println( tokenPlayer1Step);
            }
danpost danpost

2013/4/3

#
Unless the value of tokenPlayer1Step or maxWaypoints changes elsewhere then the statement will always be true and the code within the 'if' block should execute every time.
HeyHey HeyHey

2013/4/4

#
i found the reason, its nothing wrong with the code but, one subclass (Waypoint) didnĀ“t have a public void actor method. This is the reason for multiple running loops and the running of the first subclass (Basic) without reezing it into the game area. It is 100% reproducible. [Disallowed URL] Is it a Greenfoot Bug? I used the actual Version
You need to login to post a reply.