在二维数组中寻找重复值

时间:2013-07-17 20:55:13

标签: java arrays sudoku

我正在进行的任务要求我创建一个Sudoku游戏,而不使用任何类,方法,封装等。我是Java的新手。我无法验证用户输入到“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!);
        }
 }

然而这不起作用。我想遍历数组以查找任何重复的值,如果没有,则打印出“Sudoku!”如果有任何重复值,那么我想打印出“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!");
                    }
            }
        }
    }
}

2 个答案:

答案 0 :(得分:2)

由于它是家庭作业,我将尽量减少代码,但我认为如果您有关于2D数组的更多信息,你会很好的解决它,其中一些相当棘手:

  • 由于fourArray是一个数组数组,fourArray[i]指的是一个数组(您可以将其视为二维数组的第i行)。
  • 要访问数组数组中的单个整数,请使用fourArray[i][j]
  • 如果你做myArray1 == myArray2(因为你的代码目前基本上是这样),它不会比较内容;相反,它检查它们是否实际上是同一个数组(如果你先说myArray1 = myArray2就会发生这种情况)。
  • 如果您确实想要比较两个数组的内容,可以使用Arrays.equals(myArray1, myArray2)
  • 如上所述,fourArray.length是一维的大小; fourArray[x].length是其他维度的大小(x无关紧要,只要它在0fourArray.length - 1之间。

在回复评论时添加:我的理解和假设是您试图避免二维fourArray中包含的任何值之间的任何重复值。有很多解决方案。

可能被称为天真的解决方案是首先使用一对嵌套的for循环来遍历fourArray中的每个值。对于每个值,将其与其他每个值进行比较。您的中间代码可能如下所示:

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.
    }
}

一方面,这有点低效。另一方面,对于小型2-D阵列来说,它仍然是完全无足轻重的计算,所以如果它对你有意义,那么就去做其他问题。

但是,如果您感兴趣并假设允许的值是顺序集的一部分,我会提交另一个想法供您考虑(即,在典型的数独游戏中,您有3x3个框,其中允许的值是总是1-9,永远不会更高)。如果您有一个数组count[]来跟踪这些已知值发生了多少次,该怎么办?因此,其中的所有值都将初始化为零。当您遍历表中的每个点时(如上面的代码示例所示),您可以使用找到的值 - 将其称为v - 来增加count[v]count[]中任何大于1的值都表示重复。

答案 1 :(得分:0)

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

第二件事就像你想的那样,假设我正确理解了问题,你的双循环是错误的。如果要检查网格中每个其他值的每个值,我就会这样做:

    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!");
    }
相关问题