1

我正在处理的任务要求我在不使用任何类、方法、封装等的情况下创建数独游戏。我无法验证用户输入到“fourArray”或“nineArray”中的值是否不包含重复值。到目前为止,我一直在尝试使用嵌套的 for 循环来遍历任一数组的列和行。例如,我一直试图在我的程序末尾包含以下代码,以确定是否有任何重复值:

for (int i = 0; i < fourArray.length; i++) {
    for (int j = i + 1; j < fourArray.length; j++)
        if (fourArray[i] == fourArray[j]) {
            System.out.println("No Sudoku");
        } else {
            System.out.println("Sudoku!);
        }
 }

但是,这是行不通的。我想遍历数组以查找任何重复的任何值,如果没有,则打印出“数独!” 如果有任何重复的值,那么我想打印出“数独!” 我需要对数组进行排序吗?还是有一些我不知道的方法?我已经包含了我的程序。感谢您的时间。

import java.util.Scanner;


public class Sudoku {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int boardSize = -1;
        int[][] fourArray = { {0,0,0,0}, {0,0,0,0}, {0,0,0,0}, {0,0,0,0} };
        int[][] nineArray = { {0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0} }; 
        while (true)
        {
            Scanner boardsizeOption = new Scanner(System.in);
            System.out.println("Please select a board size:" + "\n" + "1) 4x4" + "\n" + "2) 9x9");
            boardSize = boardsizeOption.nextInt();
            if (boardSize == 1 || boardSize == 2) {
                break;
            }
        }
        if (boardSize == 1) { //still need to build complete board 
            int i, j = 0;
            for (i = 0; i < fourArray.length; i++)
            {
                for (j = 0; j < fourArray.length; j++)
                    System.out.print(fourArray[i][j] + " ");
                System.out.println();
            }
        } else if (boardSize == 2) { 
            int i, j = 0;
            for (i = 0; i < nineArray.length; i++)
            {
                for (j = 0; j < nineArray.length; j++)
                    System.out.print(nineArray[i][j] + " ");
                System.out.println();
            }
    }
        int dataSelection = -1;     
        while (true)
        {
            Scanner rowColumn = new Scanner(System.in);
            System.out.println("Please select which way you would like to enter the values:" + "\n" + "1) row" + "\n" + "2) columnn");
            dataSelection = rowColumn.nextInt();
            if (dataSelection == 1 || dataSelection == 2) {
                break;
            }
        }
        //Entering by ROWS
        //This is for a 4x4 board size using rows
        if (dataSelection == 1) {
            if (boardSize == 1) {
                int row = 1;
                while (row < 5) {
                    String row1Values4x4 = "-1";
                    while (true) {
                        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) {
                            break;
                        }
                    }
                    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++;
                }
                //This is for a 9x9 board size using rows 
                } else { 
                    int row = 1;
                    while (row < 10) {
                        String row1Values9x9 = "-1";
                        while (true) {
                            Scanner firstRow9x9 = new Scanner(System.in);
                            System.out.println("Please enter nine values using commas for row " + row); //this needs to loop
                            row1Values9x9 = firstRow9x9.next();
                            row1Values9x9 = row1Values9x9.replaceAll(" ",""); //this is in case user enters numbers with spaces
                            if (row1Values9x9.length() == 17) {
                                break;
                            }
                        }
                        String strArray[] = row1Values9x9.split(",");
                        int arraySidesInteger[] = new int[strArray.length];
                        for (int i = 0;  i < strArray.length;  i++) {
                            arraySidesInteger[i] = Integer.parseInt(strArray[i]);
                        }
                        nineArray[row-1] = arraySidesInteger;
                        for (int i = 0; i < nineArray.length; i++) {
                            for (int j = 0; j < nineArray.length; j++)
                                System.out.print(nineArray[i][j] + " ");
                            System.out.println();
                        }
                        row++;
                    }
                }
            //Entering by COLUMNS
            //This is for 4x4 board size using columns 
            } else { 
                if (boardSize == 1) {
                    int column = 1;
                    while (column < 5) {
                        String column1Values4x4 = "-1"; 
                        while (true) {
                            Scanner firstColumn4x4 = new Scanner(System.in);
                            System.out.println("Please enter four values using commas for column " + column);
                            column1Values4x4 = firstColumn4x4.next();
                            column1Values4x4 = column1Values4x4.replaceAll(" ","");
                            if (column1Values4x4.length() == 7) {
                                break;
                            }
                        }
                        String strArray[] = column1Values4x4.split(",");
                        int arraySidesInteger[] = new int[strArray.length];
                        for (int i = 0;  i < strArray.length;  i++) {
                            arraySidesInteger[i] = Integer.parseInt(strArray[i]);
                        }
                        for (int i = 0; i < arraySidesInteger.length; i++) {
                            fourArray[i][column-1] = arraySidesInteger[i];
                        }
                        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();
                        }
                        column++;
                    }
                //This is for a 9x9 board size using columns
                } else { 
                    int column = 1;
                    while (column < 10) {
                        String column1Values9x9 = "-1";
                        while (true) {
                            Scanner firstColumn9x9 = new Scanner(System.in);
                            System.out.println("Please enter nine values using commas for column " + column);
                            column1Values9x9 = firstColumn9x9.next();
                            column1Values9x9 = column1Values9x9.replaceAll(" ","");
                            //row1Values4x4 = row1Values4x4.replaceAll(",","");
                            if (column1Values9x9.length() == 17) {
                                break;
                            }
                        }
                        String strArray[] = column1Values9x9.split(",");
                        int arraySidesInteger[] = new int[strArray.length];
                        for (int i = 0;  i < strArray.length;  i++) {
                            arraySidesInteger[i] = Integer.parseInt(strArray[i]);
                        }
                        for (int i = 0; i < arraySidesInteger.length; i++) {
                            nineArray[i][column-1] = arraySidesInteger[i];
                        }
                        for (int i = 0; i < nineArray.length; i++) {
                            for (int j = 0; j < nineArray.length; j++)
                                System.out.print(nineArray[i][j] + " ");


                    System.out.println();
                    }
                    column++;
                }
            }
            for (int i = 0; i < fourArray.length; i++) {
                for(int j = i + 1; j < fourArray.length; j++) {
                    if(fourArray[i] == fourArray[j]) {
                        System.out.println("No Sudoku");
                    } else {
                        System.out.println("Sudoku!");
                    }
            }
        }
    }
}
4

2 回答 2

4

Since it's homework, I'm going to minimize code, but I think you'll be fine to figure it out if you have some more info about 2D arrays, some of which is fairly tricky:

  • Since fourArray is an array of arrays, fourArray[i] refers to an array (you can think of it as the i-th row of your 2-D array).
  • To access an individual integers within your array of arrays, use fourArray[i][j].
  • If you do myArray1 == myArray2 (as your code essentially does at the moment), it does NOT compare the contents; rather, it checks if they are actually the same array (as would happen if you said myArray1 = myArray2 first).
  • If you did want to compare the contents of two arrays, you can use Arrays.equals(myArray1, myArray2).
  • As follows from the above points, fourArray.length is the size in one dimension; fourArray[x].length is the size in the other dimension (where x doesn't matter so long as it's between 0 and fourArray.length - 1).

Added in response to comment: My understanding and assumption is that you are trying to avoid any duplicate values between any of the values contained in the 2-D fourArray. There are a number of solutions.

What might be called the naive solution is to first use a pair of nested for loops to go through each value in fourArray. For each of those values, compare it to every other value. Your intermediate code might look something like this:

for (int i = 0; i < fourArray.length; i++) {
    for (int j = 0; j < fourArray[i].length; j++) {
        // TODO: Compare value at (i,j) to every other point by nesting
        // two more for loops with new iterators (called, e.g., m and n)
        // TODO: If a duplicate is found, either stop searching, or at
        // least mark that a duplicate has been found somehow.
    }
}

On one hand, this is a bit inefficient. On the other, for small 2-D arrays it's still totally trivial computationally, so if it makes sense to you, do it and move on to other problems.

However, I'll submit another idea for your consideration, should you be interested and with the assumption that the allowed values are part of a sequential set (i.e., in typical Sudoku games you have 3x3 boxes within which the allowed values are always 1-9, never higher). What if you had an array count[] that kept track of how many times these known values had occurred? So all values in it would be initialized to zero. As you iterated through each spot in the table (as shown in the above code sample), you could use the value found--call it v--to increment count[v]. Any value in count[] greater than one represents a duplicate.

于 2013-07-17T23:47:50.690 回答
0

首先,在你的整个类代码中,你需要将你的数独检查向下移动 1 }(它之后应该只有 2,主电源和类)。

第二件事就像您想的那样,假设我正确理解了问题,您的双 for 循环是错误的。如果您想将每个值与网格中的每个其他值进行对比,我会这样做:

    boolean sudoku = true;
    for (int i = 0; i < fourArray.length; i++) {
        for (int j = 0; j < fourArray[i].length; j++) {
            if (fourArray[i] == fourArray[j]) {
                sudoku = false;
                break;
            } 
            if (!sudoku){
                break;
            }
        }
    }
    if (sudoku){
        System.out.println("Sudoku!");
    } else {
        System.out.println("No Sudoku!");
    }
于 2013-07-17T21:20:57.097 回答