如何打破嵌套循环

时间:2015-04-24 21:22:01

标签: java arrays loops

我目前正在制作一个程序,其中需要嵌套循环来搜索数组以找到数组内良好输入的位置。这是一个例子:

public void placePiece(int column) {
        boolean goodInput = false;

        while(!goodInput) {
            for(int x = 5; x >= 0; x--) {
                if(boardStatus[x][column] == 0) {

                    setRow(x);
                    boardStatus[x][column] = 1;
                    goodInput = true;
                    break;

                }else if(boardStatus[0][column] == 1) { 
                    goodInput = false;
                    break;
                }else{

                }

            }
        }
    }

该方法采用一个参数,该参数应该是该片段所在的列(由鼠标侦听器接收)。如果2D数组中的列已经填充到顶部,程序将陷入“else if”中的无限循环中,并且我不确定如何打破这个循环。如果输入错误,我怎么能突破这个循环,以便用户可以尝试将另一列作为输入。

5 个答案:

答案 0 :(得分:8)

An easy way is to use a labeled break statement.

myLabelName:
for(outer loop)
    for(inner loop)
        if(condition)
            break myLabelName;

This is useful when you'd rather not waste time iterating over other objects/items when you've found what you needed.


To expand, when you use just break; (without a label) it will exit the parent loop. Ex:

myLabelName:
for(outer loop){
    for(inner loop){
        if(condition){
            break myLabelName; //breaks outer loop
        }
        else if(other condition){
            break; //breaks parent (inner/nested) loop
        }
     }
 }

答案 1 :(得分:0)

Change the parameter AFTER the loop:

public void placePiece(int column) {
    boolean goodInput = false;

    while(!goodInput) {
        for(int x = 5; x >= 0; x--) {
            if(boardStatus[x][column] == 0) {

                setRow(x);
                boardStatus[x][column] = 1;
                goodInput = true;
                break;

            }else if(boardStatus[0][column] == 1) { 
                goodInput = false;
                break;
            }else{

            }

        }
        goodInput = true; // <-----
    }
}

答案 2 :(得分:0)

When you are in elseif loop your are again assigning the goodInput = false; so the while conditions again becomes true and loops continuous forever.

if you want to take input till correct input is received can use do(){}while() loop

答案 3 :(得分:0)

Based on the description you provided, I am not sure of the purpose of the else if statement. As I understand you intention, you are checking all of the rows within a certain column to see if that cell has been set or not?

The combination of the while loop and the for loop seems suspect as well. There doesn't seem to be any need for the while loop in this case.

What is supposed to happen if the entire column is filled? Should this method return a status flag?

Can't this be implemented with a single for loop?

for (int x = 5; x >= 0; --x)
{
    if(boardStatus[x][column] == 0) {
        setRow(x);
        boardStatus[x][column] = 1;
        break;
    }
}

If you want, you can track a flag variable to indicate if you ever found a cell which was empty.

答案 4 :(得分:0)

你可以使用flag或者你可以使用goto语句

标志说明

code

bool  flag = true;
for (int i = 0; (i < 10) && (flag==true); i++)
{
   for(int j = 0; (j < 10) && (flag==true); j++)
   {
      if (condition)
         flag = false;
   }  
}