Java 编写一个静态方法,检查数组是否包含所有数字1到9

Java 编写一个静态方法,检查数组是否包含所有数字1到9,java,arrays,Java,Arrays,我无法使程序正确运行。我使用了调试器,在我的程序检查了表1中的9个数字后,它尝试重新计算表1,当表1中的所有数字都是1-9时,它会跳转为false 以下是作业说明: 编写一个执行以下操作的程序: 声明一个包含3行3列的整数的二维数组。用1到9之间的数字填充数组。没有用户输入。您只需复制/粘贴以下行: int[][] table1 = { { 3, 2, 5}, {1,9,6}, {7, 8 4} }; // this one is true int[][] table2 = { {

我无法使程序正确运行。我使用了调试器,在我的程序检查了表1中的9个数字后,它尝试重新计算表1,当表1中的所有数字都是1-9时,它会跳转为false

以下是作业说明:

编写一个执行以下操作的程序:

声明一个包含3行3列的整数的二维数组。用1到9之间的数字填充数组。没有用户输入。您只需复制/粘贴以下行:

int[][]  table1 = {  { 3, 2, 5}, {1,9,6}, {7, 8 4}  };  // this one is true
int[][]  table2 = {  { 3, 3, 5}, {1,9,6}, {3, 8 4}  };  // this one is false
int[][]  table3 = {  { 3, 2, 5}, {1,9,6}, {3, 8 4}  };  // this one is false
int[][]  table4 = {  { 3, 2, 1}, {10,4,6}, {5,7,8}  };  // this one is false
int[][]  table5 = {  { 4, 1, 5}, {1,9,6}, {7, 8 4}  };  // this one is false
编写一个静态方法,检查数组是否包含所有数字1到9。(每个只能出现一次。)将数组传递给方法。该方法必须返回布尔值,如果数组中出现所有数字,则返回true,否则返回false

调用方法应如下所示:

if( allNinePresent(table1)) {
 syso( “All 9 are there in table 1”);
} else {
Syso(“ All 9 are NOT there in table 1 “);
}
private static boolean allNinePresent(int[][] array){  
============================

您的方法头应该如下所示:

if( allNinePresent(table1)) {
 syso( “All 9 are there in table 1”);
} else {
Syso(“ All 9 are NOT there in table 1 “);
}
private static boolean allNinePresent(int[][] array){  
========================

解决这个问题有很多策略。你可以选择任何对你最有意义的。这里有一些选择

编写一个将二维数组转换为一维数组的方法。这更容易处理。对1d数组排序,然后循环查找数字1-9

这是我最喜欢的。编写一个返回布尔值的方法。它需要一个数字和一个2d数组。静态布尔包含(int a,int[][]数组)如果在数组中找到“a”,则返回true。您可以调用该方法9次,以验证数组中的每个数字1–9

做一个循环,查找1-9中的每个数字。当其中一个未找到时,返回false。在所有被发现后,返回true

使用其中一个集合类和contains()方法查看它们是否都存在

这是我的密码:

public static void main(String[] args) {

    int[][] table1 = { { 3, 2, 5 }, { 1, 9, 6 }, { 7, 8, 4 } }; // this one
                                                                // is true

    int[][] table2 = { { 3, 3, 5 }, { 1, 9, 6 }, { 3, 8, 4 } }; // this one
                                                                // is false

    int[][] table3 = { { 3, 2, 5 }, { 1, 9, 6 }, { 3, 8, 4 } }; // this one
                                                                // is false

    int[][] table4 = { { 3, 2, 1 }, { 10, 4, 6 }, { 5, 7, 8 } }; // this one
                                                                    // is
                                                                    // false

    int[][] table5 = { { 4, 1, 5 }, { 1, 9, 6 }, { 7, 8, 4 } }; // this one
                                                                // is false
    {

        if (allNinePresent(table1)) {
            System.out.println(" All 9 are there in table 1 ");
        } else {
            System.out.println(" All nine are NOT there in table 1 ");
        }

    }
}

private static boolean allNinePresent(int[][] table1) {

    int[] oneDArray = new int[9];

    for (int r = 0; r < table1.length; r++) {
        for (int c = 0; c < table1[r].length; c++) {
            if (table1[r][c] == oneDArray[c]) {
                return true;
            }
        }
    }
    return false;
}
publicstaticvoidmain(字符串[]args){
int[]table1={{3,2,5},{1,9,6},{7,8,4};//这个
//是真的
int[]table2={{3,3,5},{1,9,6},{3,8,4};//这个
//是假的
int[]table3={{3,2,5},{1,9,6},{3,8,4};//这个
//是假的
int[]table4={{3,2,1},{10,4,6},{5,7,8};//这个
//是
//假的
int[]table5={{4,1,5},{1,9,6},{7,8,4};//这个
//是假的
{
如果(所有非代表(表1)){
System.out.println(“所有9个都在表1中”);
}否则{
System.out.println(“表1中没有所有九个”);
}
}
}
私有静态布尔allNinePresent(int[][]表1){
int[]oneDArray=新int[9];
对于(int r=0;r

任何帮助都将不胜感激。

这里有两个提示。一个是来自评论


“记住int[]oneDArray=new int[9];将数组初始化为所有0。此外,考虑到我所说的,一旦表1包含0,您就会返回true。这是我的提示,但我不会做您的家庭作业–Tommaso Pasini”

第二个提示是确保您已经彻底阅读了以下内容:

“这是我的最爱。请编写一个返回布尔值的方法。它将使用一个数字和一个2d数组。静态布尔值包含(int a,int[][]数组)。如果在数组中找到“a”,则返回true。您可以调用该方法9次,以验证数组中是否有每个数字1–9。”


如果你需要更多的帮助,你也可以考虑找个家庭教师:)

一个选择是这样的

private static boolean allNinePresent(int[][] array) {

    // convert to one dimensional array
    int[] input_array = new int[9];
    int k = 0;
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            input_array[k++] = array[i][j];
        }
    }

    // count occurences of numbers
    int[] result = new int[9];
    for (int i = 0; i < 9; i++) {
        int val = input_array[i];
        if (val < 10) {
            result[val - 1] = result[val - 1] + 1;
        }
    }

    // check if value occurs exactly once
    boolean ret = true;
    for (int i = 0; i < 9; i++) {
        if (result[i] != 1) {
            ret = false;
        }
    }
    return ret;
}
私有静态布尔allNinePresent(int[][]数组){
//转换为一维数组
int[]输入_数组=新的int[9];
int k=0;
对于(int i=0;i<3;i++){
对于(int j=0;j<3;j++){
输入_数组[k++]=数组[i][j];
}
}
//统计数字的出现次数
int[]结果=新的int[9];
对于(int i=0;i<9;i++){
int val=输入_数组[i];
如果(val<10){
结果[val-1]=结果[val-1]+1;
}
}
//检查值是否恰好出现一次
布尔ret=真;
对于(int i=0;i<9;i++){
如果(结果[i]!=1){
ret=假;
}
}
返回ret;
}

由于我不想提供完整的解决方案,以下是我的策略建议:

使用9个布尔数的数组,它告诉你是否已经找到了每个数字

(例如:
found[0]==true
告诉您已经找到数字1)


使用并更新数组,在表中循环,检查每个数字是否在1-9之间,是否尚未找到。

可以使用递归检查每个值

private static boolean allNinePresent(int[][] table) {
    int size = 0; //used to initialize the size of the array
    for (int[] i : table) {
        size = size + i.length; //calculate the total size of the 2d array
    }

    if (size == 9) { //check that the array contains exactly 9 elements
        return recursiveCheck(table, 1); 
    }
    else {
        return false;
    }
}

/**
 * Recursively check the int array for the control number.
 * @param test - The array that needs to be tested.
 * @param control - The control number to be tested against.
 * @return the result of the recursive check.
 */
private static boolean recursiveCheck(int[][] value, int control) {
    for (int[] iArr : value) { //Extract every int array in the 2d array
        for (int i : iArr) { //Test every digit in the int array
            if (i == control) {
                if (control != 9) { //recursive check if control haven't reached 9
                    return recursiveCheck(value, control + 1);
                }
                else {
                    return true;
                }
            }
        }
    }
    return false; //this will only be reached if all values in the int array  
                  // is tested false
}

你真的相信,通过让我们为你做作业,有一天你就能编写自己的代码了吗?你的代码实现了上述哪一种策略?记住int[]oneDArray=new int[9];将数组初始化为所有0。此外,考虑到我所说的,只要表1包含0,您就会返回true。这是我的暗示,但我不会做你的家庭作业。我不知道为什么讲师的“最爱”是他的最爱;那肯定不是我的。当O(n)算法的代码同样清晰时,为什么要故意选择O(n²)算法?@DrA:请提交DCMA取下请求;看。版主无法核实版权侵权者