简体   繁体   中英

Java Nested Loop (While)

Hey all beginner here,

Been stuck here for awhile. Troubleshooting I either get an infinite loop or it only loops 11 times, (I want the outside to loop 21 times). Basically I read this as the first loop will be executed since it will be true, the second loop will be executed ten times until it is not true. Then the outside loop will continue to run by-passing the inner loop (since the condition is no longer valid) until the outside condition is no longer true.

Thanks in advance!

public void setup33()
    {
        int x = 0;
        int y = 0;
        int i = 0;

        while (i <21)
        {
            int x2 = 300;
            int y2 = 100;
            int size = 10;
            addObject( new Bubble(), x, y);
            x = x + 45;
            y = y + 30;


            while (i < 10)
            {     

                addObject(new Bubble(size), x2,y2);
                x2 = x2 + 40;
                size = size + 10;

            }
            i++;
        }

    }

you shouldn't use the same i as your variable for while loops if you are trying to make each loop discrete. You may want this?

public void setup33()
    {
        int x = 0;
        int y = 0;
        int i = 0;
        int j = 0;

        while (i <21)
        {
            int x2 = 300;
            int y2 = 100;
            int size = 10;
            addObject( new Bubble(), x, y);
            x = x + 45;
            y = y + 30;


            while (j < 10)
            {     

                addObject(new Bubble(size), x2,y2);
                x2 = x2 + 40;
                size = size + 10;
                j++;
            }
            i++;
        }

    }

Otherwise your problem is you forgot to update i where j++ is.

You need another variable (in the inner loop) which you can modify, or the inner loop will modify the outer loop state ( i is i , add a j ). You can also declare multiple variables of the same type in one statement and use a shorter method for addition. Like,

while (i < 21) {
    int x2 = 300, y2 = 100, size = 10, j = 0;
    addObject(new Bubble(), x, y);
    x += 45;
    y += 30;

    while (j < 10) {
        addObject(new Bubble(size), x2, y2);
        x2 += 40;
        size += 10;
        j++;
    }
    i++;
}

You have infinity loop in 2nd loop, so you have to add a variable to count this loop:

    while (i <21)
    {
        int x2 = 300;
        int y2 = 100;
        int size = 10;
        addObject( new Bubble(), x, y);
        x = x + 45;
        y = y + 30;

        int j = 0;
        while (i < 10 && j < 10)
        {     
            addObject(new Bubble(size), x2,y2);
            x2 = x2 + 40;
            size = size + 10;
            j++;
        }
        i++;
    }

Result: 1st loop will run 21 times, and 2nd loop run 10 times in each first 10 times of 1st loop.

The way I'm interpreting it, you ONLY want to run the sub while-loop for the first 10 iterations of the top level loop.

In this case, a second loop is not needed, and instead you should use an if statement.

public void setup33()
{
    int x = 0;
    int y = 0;
    int i = 0;

    while (i <21)
    {
        int x2 = 300;
        int y2 = 100;
        int size = 10;
        addObject( new Bubble(), x, y);
        x = x + 45;
        y = y + 30;

        if (i < 10)
        {     
            addObject(new Bubble(size), x2,y2);
            x2 = x2 + 40;
            size = size + 10;

        }
        i++;
    }
}

Otherwise, if you want to run the sub loop 10 times for every run of the top level loop, you need to use a second variable for the sub loop.

public void setup33()
{
    int x = 0;
    int y = 0;
    int i = 0;
    int j = 0;

    while (i <21)
    {
        int x2 = 300;
        int y2 = 100;
        int size = 10;
        addObject( new Bubble(), x, y);
        x = x + 45;
        y = y + 30;

        while (j < 10)
        {     
            addObject(new Bubble(size), x2,y2);
            x2 = x2 + 40;
            size = size + 10;
            j++;
        }
        j = 0;
        i++;
    }
}

You can also use a do while loop, but you need to add an extra variable there. Ex:

 if (i < 21) {
        do {
            int x2 = 300;
            int y2 = 100;
            int size = 10;
            addObject(new Bubble(), x, y);
            x = x + 45;
            y = y + 30;

            while (j < 10) {
                addObject(new Bubble(size), x2, y2);
                x2 = x2 + 40;
                size = size + 10;
                j++;
            }
            j = 0;
            i++;
        } while (i < 21);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM