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

2011/10/10

While loops

kiarocks kiarocks

2011/10/10

#
I am making a scenario that displays dots. i would like to compact having several while loops to just having one to add the dots.
danpost danpost

2011/10/10

#
@kiarocks, what while loops do you have so far?
kiarocks kiarocks

2011/10/10

#
while(addeddots != numdots)
                {
                    addeddots ++; 
                    addObject(new Dot(5), ((addeddots-119) * 5) - 1, 5);
                }
repeated like this again
while(addeddots != numdots && addeddots >= 119)
                {
                    addeddots ++; 
                    addObject(new Dot(5), ((addeddots-119) * 5) - 1, 11);
                }
again
while(addeddots != numdots && addeddots >= 238)
                {
                    addeddots ++; 
                    addObject(new Dot(5), ((addeddots-238) * 5) - 1, 17);
                }
danpost danpost

2011/10/10

#
@kiarocks, does the value of numdots change? or does it contain a constant value? and, if constant, what is its value? Also, does the value of addeddots start at zero? Basically, my question is: What are the values of numdots and addeddots going in to each while loop?
danpost danpost

2011/10/10

#
BTW, the condition of addeddots within the while statement should never change as written. That is, you are checking to see if addeddots is greater than (or equal to) a number, and then incrementing addeddots within the loop; it will always be greater than that number, and therefore only exit the loop when addeddots becomes equal to numdots. Either it could be coded more efficiently on that count as well, or maybe you meant 'less than or equal to'!?! Something else: in all three loops you are subtracting either 119 or 238 from addeddots, which is OK, I guess, for the second and third loops given above. But for the first loop, if addeddots is less than or equal to 119, your X-coordinate in adding the new Dot object will be negative.
danpost danpost

2011/10/10

#
If you are just trying to add 119 dot objects at each of 3 different horizontal locations, the code could be simply:
for (int myCt = 0; myCt < 119; myCt++) {
    addObject(new Dot(5), myCt * 5 + 4, 5);
    addObject(new Dot(5), myCt * 5 + 4, 11);
    addObject(new Dot(5), myCt * 5 + 4, 17);
}
kiarocks kiarocks

2011/10/10

#
ok, let me upload the scenario.
kiarocks kiarocks

2011/10/10

#
You need to login to post a reply.