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

2014/3/1

Can anyone help a newbie figure out how to populate the world using a while loop

kzg0015 kzg0015

2014/3/1

#
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
danpost danpost

2014/3/1

#
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.
1
while(/* condition */) { /* execute this */ }
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).
danpost danpost

2014/3/1

#
Java has a trail (tutorial) called Language Basics that you maybe should take a look at.
kzg0015 kzg0015

2014/3/1

#
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)); } }
danpost danpost

2014/3/1

#
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).
kzg0015 kzg0015

2014/3/1

#
thank you; that did the job
You need to login to post a reply.