开发者

why are my variable not adding together in these for statements?

开发者 https://www.devze.com 2022-12-21 07:16 出处:网络
I\'m working on some beginning Java logic, and I\'m not sure why this is not working.Here is a method I made:

I'm working on some beginning Java logic, and I'm not sure why this is not working. Here is a method I made:

private void printSubclassBoxes(){
        int  coordinateX = ((getWidth() - BOX_WIDTH) /4);

        for ( int i = 0; i < 3; i++){

            double c开发者_StackOverflow社区oordinateY = (getHeight() / 2);     
            GRect classBox = new GRect (coordinateX, coordinateY, BOX_WIDTH, BOX_HEIGHT);

            GLabel classLabel = new GLabel ("Program");
            double labelCoordinateX = (coordinateX + ((classBox.getWidth() / 2) - (classLabel.getWidth() / 2)));
            double labelCoordinateY = (coordinateY + ((classBox.getHeight() / 2) + (classLabel.getAscent() / 2)));
            add(classBox);
            add(classLabel, labelCoordinateX, labelCoordinateY);
            if (i == 1){
            coordinateX = (((getWidth() - BOX_WIDTH) /4) * 2);
        } 
        if (i == 2){
            coordinateX = (((getWidth() - BOX_WIDTH) /4) * 3);
        } 

        }   

    }

Now I'm sure there are probably better ways to do this, but please-I'm not interested in that right now (I'm trying to learn without being spoonfed the answers). All I am wanting to know is why the ending two if statements are not working like I think they should.

For simplicity sake, let's say

100 = ((getWidth() - BOX_WIDTH)

int coordinateX = 25;

My understanding is that int i gets to that first if statement and adds 25 + 25 so then coordinateX = 50.

then next time in the loop, i = 2 so coordinateX would = 75.

This is what I'm expecting to happen, but it's not. I seem to be printing the first two boxes directly on top of each other, and then the third is moving 25.

Thanks for your help guys. Now that I got that loop figured out, I went ahead and solved it a different way. I ended up assigning coordinateX to another variable and using that to add to the end:

int  coordinateX = ((getWidth() - BOX_WIDTH) /4);
        int otherCoordinateX = coordinateX;

        for ( int i = 0; i < 3; i++){

            double coordinateY = (getHeight() / 2);     
            GRect classBox = new GRect (coordinateX, coordinateY, BOX_WIDTH, BOX_HEIGHT);

            GLabel classLabel = new GLabel ("Program");
            double labelCoordinateX = (coordinateX + ((classBox.getWidth() / 2) - (classLabel.getWidth() / 2)));
            double labelCoordinateY = (coordinateY + ((classBox.getHeight() / 2) + (classLabel.getAscent() / 2)));
            add(classBox);
            add(classLabel, labelCoordinateX, labelCoordinateY);
            coordinateX = otherCoordinateX + coordinateX;

        }   


at the first iteration of the loop:

  • i = 0, and
  • coordinateX = 25, as expected.

At the end of the iteration coordinateX is not updated (since i == 0).

Then, at the second iteration:

  • i = 1,
  • but still coordinateX = 25 since it wasn't updated.

At the end of the second iteration coordinateX is updated because of the if i == 1) test.

In the third iteration

  • i = 2
  • the new value of coordinateX is used, which was set at the end of the second iteration.

Solution

To not change your code in any essential way, simply replace if (i == 1) by if (i == 0), and if (i == 2) by if (i == 1).

EDIT: Lesson

In the for(...) { } declaration, the i < 3 test is checked at the beginning of each iteration, but the i++ part is called at the end of each iteration.


You just have an off-by-one loop index problem. The first time through your loop, i is 0, not 1. The second time through, it's 1, not 2. Changing your lines if (i == 1) and if (i == 2) to if (i == 0) and if (i == 1), respectively, will fix your problem.

It's still a weird way to do the loop, though. A more standard way to do it would be to set up an incrementX variable, for example, and then just add it unconditionally each time through the loop.


Since you want to increment the value before the "next iteration" you should do it between first/second and second/third but you put update of the variable coordinateX at the end of the loop so you should check different indexes: 0 and 1.

What does it mean?

This works:

for ( int i = 0; i < 3; i++)
{
  /* other part of the loop */  
  if (i == 0)
    coordinateX = (((getWidth() - BOX_WIDTH) /4) * 2);
  else if (i == 1)
    coordinateX = (((getWidth() - BOX_WIDTH) /4) * 3);
}  

but this works too:

for ( int i = 0; i < 3; i++)
{
  if (i == 1)
    coordinateX = (((getWidth() - BOX_WIDTH) /4) * 2);
  else if (i == 2)
    coordinateX = (((getWidth() - BOX_WIDTH) /4) * 3);
  /* other part of the loop */  
}  


May seem like a lame answer but this is what I'd do in these situations. Put a bunch of print statements in so when it loops you can verify the output you get is what you are expecting. If it's not at a certain point, start trying to determine why.

0

精彩评论

暂无评论...
验证码 换一张
取 消