73.9k views
5 votes
JAVA PROJECT USING ( WHILE LOOP )

Bubble 1 and 2 are moving right 1 position.
Bubble 1 and 2 are moving right 1 position.
Bubble 1 and 2 are moving right 1 position.

Bubble 1 and 2 are moving left 1 position.

Bubble 1 and 2 are moving left 1 position.

Bubble 1 and 2 are moving left 1 position.

This is pass 2.
Bubble 1 and 2 are moving right 1 position.
Bubble 1 and 2 are moving right 1 position.
Bubble 1 and 2 are moving left 1 position.

Bubble 1 and 2 are moving left 1 position.

This is pass 3.
Bubble 1 and 2 are moving right 1 position.
Bubble 1 and 2 are moving left 1 position.



This is pass 4.
Bubble 1 is not moving.
Bubble 2 is not moving.

1 Answer

2 votes

Step-by-step explanation:

so, what's the question/problem ?

you want to know where the bubbles are after all 4 passes ? right at the original position.

you want to know, how such a while loop could look like ?

int i = 3;

// outer count-down to limit the passes

while (i >= 0) // making 4 passes

{

int j = i;

// inner count-down to limit the right moves

while (j > 0)

{

move(bubble1, right, 1);

move(bubble2, right, 1);

j = j - 1; // count-down

}

j = i;

// inner count-down to limit the left moves

while (j > 0)

{

move(bubble1, left, 1);

move(bubble2, left, 1);

j = j - 1; // count-down

}

i = i - 1; // outer count-down

}

User Johngreen
by
6.1k points