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

2013/8/9

loops

davemib123 davemib123

2013/8/9

#
Hi, having a try with loops and have come to a slight problem. I have read that their are 3 types of loops, I have managed to get the for and while working, but do while doesnt work as the previous two. The first two generate 3 worms and if the number falls below 3 another worm is added. Whereas do while keeps adding them. This is my attempt:
        //loop1
        while (getObjects(Worm.class).size() < 3) {
            addObject(new Worm(), Greenfoot.getRandomNumber(530), Greenfoot.getRandomNumber(500));
        } 

        //loop2
        for (int i = getObjects(Worm.class).size(); i < 3; i++) {  
            addObject(new Worm(), Greenfoot.getRandomNumber(530), Greenfoot.getRandomNumber(500));
        }

        //loop3
        do
        {
            addObject(new Worm(), Greenfoot.getRandomNumber(530), Greenfoot.getRandomNumber(500));
        }
        while (getObjects(Worm.class).size() < 3);
Any suggestions ?
bourne bourne

2013/8/9

#
"the statements within the do block are always executed at least once" http://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html
davemib123 davemib123

2013/8/9

#
yea I read that, but how do I stop the worms spawning for loop 3. I've tried this, but it just crashes greenfoot:
int i = getObjects(Worm.class).size();
        do
        {
           addObject(new Worm(), Greenfoot.getRandomNumber(530), Greenfoot.getRandomNumber(500));
          i++;
        }
        while (i < 3);
danpost danpost

2013/8/9

#
When you use a do-while loop, it always will execute once each time before checking the condition. You want the condition to be checked before executing the code; so you should use a basic while loop. Actually better would be to use a standard if statement:
if (getObjects(Worm.class).size() < 3) addObject(new Worm(), Greenfoot.getRandomNumber(530), Greenfoot.getRandomNumber(500));
Even if two are removed in the same act cycle, have one replaced each act cycle would not be noticeably different than replacing both at the same time.
davemib123 davemib123

2013/8/10

#
thanks for the info Danpost, but I was trying to figure how to use do-while for the same solution.
danpost danpost

2013/8/10

#
Because do-while works differently than while, you cannot expect them to be used for the same thing. However, with slight modification of your code it could be done:
if (getObjects(Worm.class).size() < 3)
{
    do
    {
        addObject(new Worm(), Greenfoot.getRandomNumber(530), Greenfoot.getRandomNumber(500));
    } while getObjects(Worm.class).size() < 3);
}
This will add the beforehand check while maintaining a do-while loop.
danpost danpost

2013/8/10

#
Another way to handle it is this:
Worm worm = null; // to reference new worms
do
{
    worm = new Worm();
    addObject(worm, Greenfoot.getRandomNumber(530), Greenfoot.getRandomNumber(500));
} while getObjects(Worm.class).size() < 4); // raised to 4
removeObject(worm); // removing last worm added
Although this method would use up more resources than necessary, it will keep the number of worms at three while maintaining a do-while loop.
davemib123 davemib123

2013/8/10

#
that's great. thanks danpost :)
You need to login to post a reply.