i have been using greenfoot for about a month and since i don't know java i don't know how to do anything. Any way how can i populate my world using a while loop
Did you want to put a condition on populating the world? or are you just trying to learn how to create a loop using the 'while' keyword? I ask because you need a boolean argument (whether a value or expression) to be used with the 'while' keyword.
with the 'condition' returning a boolean value and the 'execute this' being populating the world.
The loop will repeat as long as 'condition' returns a 'true' value. That is, unless you 'break' from the loop from within (which should also be conditional).
i just want to know how to add to 10 worms, 5 crabs, and 2 Lobsters using the populateWorld method... this is what i have but it dose absolutely nothing
public void populateWorld()
{
int k=0;
while(k<5)
{
addObject(new Crab(), Greenfoot.getRandomNumber(560), Greenfoot.getRandomNumber(560));
}
int i=0;
while(i<2)
{
addObject(new Lobster(), Greenfoot.getRandomNumber(560), Greenfoot.getRandomNumber(560));
}
int j=0;
while (j<10)
{
addObject(new Worm(), Greenfoot.getRandomNumber(560), Greenfoot.getRandomNumber(560));
}
}
Try 'while ((k++)<5)' and similar for 'i' and 'j'. Unless the value of the boolean turns 'false' at some point, your scenario will appear to freeze (loop infinitely).