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

2013/2/11

Placing Random Orbs

CHERNANDEZ95 CHERNANDEZ95

2013/2/11

#
Hi, I am having trouble placing five orbs in the world at random locations using a loop. My instructions are to: Within the OrbWorld constructor, use a loop to place at least five Orbs in the world whenever you run the program. Use conditions to ensure none of the Orb graphics will be cut off by the edge of the world. Assign each of the Orbs a random velocity. This is what I have so far: randomOrbs(5); } /** * Creates five golden Orbs and places them at * random locations within the world. */ public void randomOrbs(int number) { While(number > 5); { int x = Greenfoot.getRandomNumber(getWidth()); int y = Greenfoot.getRandomNumber(getHeight()); int xSpeed = Greenfoot.getRandomNumber(10); int ySpeed = Greenfoot.getRandomNumber(10); addObject (new Orb (new Velocity(int xSpeed, int ySpeed)),x, y); number++; } } }
danpost danpost

2013/2/11

#
First, 'While' is the wrong case and should be 'while'. Second, the semicolon in the line 'while(number>5);' will cause nothing to happen within the 'while' loop and, if the loop was executed, will never exit it. Next, without the semicolon, if 'number > 5' was true to begin with to execute the loop, incrementing 'number' will never cause the loop to be exited as it will continue to be greater than 5. This one line seems to be your main crutch. Please refer to the control flow section of the Java tutorials. Focus mainly on the looping statements.
CHERNANDEZ95 CHERNANDEZ95

2013/2/11

#
Thank you for the information. I will definitely be using it.
You need to login to post a reply.