0

所以我想要做的是验证我的用户只使用逗号输入数字 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;

公共类数独练习{

/**
 * @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 循环的初始化和更新。

4

2 回答 2

0

如果使用标签,中断任何级别循环的执行都非常容易

看例子

int count=0;

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

第一个循环被标记为A 并且 break 停止执行标记为A的代码块

于 2013-07-26T07:18:12.190 回答
0

请试试这个

公共静态无效主要(字符串[]参数){

    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();
    }

}
于 2013-07-26T07:10:05.363 回答