# for loop not working?

• 07-24-2012, 02:28 PM
Newbieprogrammer
for loop not working?
Hi

I'm doing the Stanford cs 106a course and am stuck on what must be a very simple oversight.

The exercise I'm doing requires a simple program to create random circles on a canvas with random radius, color etc. 10 circles. I've started the program just to test it, without worrying about getting all 10 precisely within the canvas. But there's an unexpected hitch- it only creates one circle. As far as I can see the "for" loop I've written should make 10 method calls and get ten returns- unless I've misunderstood something. I've gone back through the book etc but can't find anything that contradicts my thought process. Here's what I've written;

import acm.program.*;
import acm.graphics.*;
import acm.util.*;
import java.awt.*;

public class RandomCircles extends GraphicsProgram {

private RandomGenerator rgen = RandomGenerator.getInstance();

public void run() {

int radius = rgen.nextInt( 25, 250);
int positionx = rgen.nextInt(getWidth());
int positiony = rgen.nextInt(getHeight());
Color nextcolor = rgen.nextColor();
for (int i = 0; i < 10; i++) {
}

}

private GOval createCircle(int x, int y, int r, Color color) {
GOval circle = new GOval(x, y, r, r);
circle.setColor(color);
circle.setFilled(true);
return circle;

}
}

Could someone let me know what I've done wrong? Thanks!

Robbie
• 07-24-2012, 02:34 PM
eRaaaa
Re: for loop not working?
Many classes and methods which I dont know, but you never change the parameter for the createCircle method or?
(positionx, positiony, radius, nextcolor) are still the same!
You have to change these in your loop! Otherwise you draw ten times the same circle -> looks like one :D
• 07-24-2012, 02:38 PM
Newbieprogrammer
Re: for loop not working?
Of course! So instead of defining variables which use the random generator, I put the random generator into the brackets after the method call? Makes sense I guess, ta, I'll give it a pop...
• 07-24-2012, 02:55 PM
Newbieprogrammer
Re: for loop not working?
That worked, thanks!