如何在继续迭代嵌套for循环的同时打破while循环

时间:2013-07-26 05:31:44

标签: java arrays for-loop while-loop sudoku

所以我要做的是验证我的用户只使用逗号输入数字1-4,没有重复项。例如,1,2,3,4或2,1,4,3。我已经包含了一个while循环,以确保用户输入正确的信息。我对for循环感到困惑,因为我不确定是否需要额外的for循环才能使用i和j访问数组中的数组?我真的很困惑,我正在寻求某种帮助。我目前只有Python经验。

我意识到当我打破while循环以将值输入到数组中时,一旦我返回询问用户下一行的值,我的for循环初始化和更新已重新启动。

我还尝试将if语句更改为:if (row1Values4x4.charAt(k) > '0' && row1Values4x4.charAt(k) < '5') { 然而,这没有意义,因为当我的for循环遇到不在1-4范围内的值时,我想要突破循环。

我没有收到任何错误,它没有按照我想要的方式工作。

import java.util.Scanner;

public class sudokuPractice {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub
    int[][] fourArray = { {0,0,0,0}, {0,0,0,0}, {0,0,0,0}, {0,0,0,0} };
            int row = 1;
            int k = 0;
            while (row < 5) 
            {
                String row1Values4x4 = "-1";
                boolean done = false;
                while (!done)
                {
                    Scanner firstRow4x4 = new Scanner(System.in);
                    System.out.println("Please enter four values using commas for row " + row); //this needs to loop
                    row1Values4x4 = firstRow4x4.next();
                    row1Values4x4 = row1Values4x4.replaceAll(" ",""); //this is in case user enters numbers with spaces
                    if (row1Values4x4.length() == 7) {
                        for (k = 0; k < row1Values4x4.length();k++) {
                                if (row1Values4x4.charAt(k) < '0' && row1Values4x4.charAt(k) > '5') {
                                    done = true;
                                } //else {
                                //}
                            }
                    }
                }
                String strArray[] = row1Values4x4.split(",");
                int arraySidesInteger[] = new int[strArray.length];
                for (int i = 0;  i < strArray.length;  i++) {
                    arraySidesInteger[i] = Integer.parseInt(strArray[i]);
                }
                fourArray[row-1] = arraySidesInteger;
                for (int i = 0; i < fourArray.length; i++) {
                    for (int j = 0; j < fourArray.length; j++)
                        System.out.print(fourArray[i][j] + " ");
                    System.out.println();
                }
                row++;
                k++;
                }
}

}

//我包含了整数k,因为我试图保留for循环的初始化和更新。

2 个答案:

答案 0 :(得分:0)

可以试试这个

public static void main(String [] args){

    int row = 1;
    int k = 0;
    String row1Values4x4 = "-1";
    int[][] arraySidesInteger = { { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 } };

    while (row <= 4) {

        boolean done = false;

        while (!done) {

            Scanner firstRow4x4 = new Scanner(System.in);
            System.out.println("Please enter four values using commas for row " + row); // this needs to loop
            row1Values4x4 = firstRow4x4.next();
            String[] values = row1Values4x4.split(",");

            for (int i = 0; i < values.length; i++) { //Logic to validate the value

                if (Integer.parseInt(values[i]) < 0
                        || Integer.parseInt(values[i]) > 4) {
                    System.out
                            .println("You have entered the value which not fit in the boundary.Please Try again.");
                    done = true;
                    break;
                }
            }

            if (done) { // Loop again to get the row value correctly.
                done = false;
                continue;
            }

            //Adding the value to row
            for (int i = 0; i < values.length; i++) {
                arraySidesInteger[row - 1][i] = Integer.parseInt(values[i]);
            }

            row++;
            done = true;

        }
    }

    //Prints the array
    for (int i = 0; i < arraySidesInteger.length; i++) {
        for (int j = 0; j < arraySidesInteger.length; j++)
            System.out.print(arraySidesInteger[i][j] + " ");
        System.out.println();
    }

}

答案 1 :(得分:0)

如果使用标签

,打破任何级别的循环都很容易

查看示例

int count=0;

A: while(true){
    while(true){
        System.out.println(count++);
        if(count==10){
            break A;
        }
    }
}

第一个循环标记为 A 并且中断停止执行标记为 A

的代码块