2

I initialized the first bricks position, then made two for loops inside of which I created a GRect object, set its color and added it to the window and also adjusted the brick's location for each row and column but no bricks appear at all.

I tried using drawRect() but still no bricks appeared. I don't know what the problem is; why aren't any bricks added although the ball and the paddle are just fine?

double x = 0, y = 0;
for (int i = 0; i > ROWS; i++)
{
    for (int j = 0; j > COLS; j++)
    {
        GRect brick = newGRect(x, y, B_WIDTH, B_HEIGHT);
        setColor(brick, "RED");
        setFilled(brick, true);
        add(window, brick); 
        x = x + B_WIDTH + 10;            
    }
    y = y + B_HEIGHT + 7;
}
1
  • 1
    You can post code if it is concise and relevant to the error. If you feel it could be a design issue or something you've missed, use pseudocode. – user103853 Aug 21 '14 at 19:01
2

Look at the code which controls the loops, e.g:

for (int i = 0; i > ROWS; i++)

This could be read as:

  1. Create a for loop
  2. Set the variable i to 0.
  3. Loop as long as i is greater than ROWS.
  4. Increment i by 1.

The problem can be seen in #3. i is already set to 0, which is not greater than ROWS, and so the loop will exit immidiately, and never execute.

The correct logic would be to loop as long as i is less than ROWS, for example:

for (int i = 0; i < ROWS; i++)

This will loop as long as i is smaller than ROWS. So in the first iteration of the loop, i will be 0, which is less than ROWS, and so the body of the loop will execute. This will continue until i becomes equal to ROWS, and only then will the loop exit.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .