Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用来自其他阵列的信息创建二维栅格阵列_Java_Arrays_Methods - Fatal编程技术网

Java 如何使用来自其他阵列的信息创建二维栅格阵列

Java 如何使用来自其他阵列的信息创建二维栅格阵列,java,arrays,methods,Java,Arrays,Methods,我试着打印一个随机数的二维网格,另一个大小相等的网格,用-1代替可以被3整除的数字。我几乎完成了所有工作,但第二个网格只打印了-1。我做错了什么?我怎样才能让第二个网格只打印数字可以被3整除的-1?谢谢 public class practice { public int[][] createArray(int rSize, int cSize) { Random r = new Random(); int[][] array = new int

我试着打印一个随机数的二维网格,另一个大小相等的网格,用-1代替可以被3整除的数字。我几乎完成了所有工作,但第二个网格只打印了-1。我做错了什么?我怎样才能让第二个网格只打印数字可以被3整除的-1?谢谢

public class practice

{



   public int[][] createArray(int rSize, int cSize) {

        Random r = new Random();

        int[][] array = new int[rSize][cSize];

        for (int row = 0; row < array.length; row++) {

            for (int col = 0; col < array[0].length; col++) {

                array[row][col] = r.nextInt(25);

            }

        }

        return array;

    }

    public void print2DArray(int[][] Array) {

        for (int row = 0; row < Array.length; row++) {

            for (int col = 0; col < Array[0].length; col++) {

                System.out.print(Array[row][col] + "\t");

            }

            System.out.println("\n");

        }

    }

    public int[][] createCoords(int[][] Array) {

        int[][] coord = {

            {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1},

            {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1},

            {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1},

            {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1},

            {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1},

            {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1},

            {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1},

            {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1},

            {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1},

            {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1}

        };

        for (int row = 0; row < coord.length; row++) {

            for (int col = 0; col < coord[0].length; col++) {

                System.out.print(coord[row][col] + "\t");

            }

            System.out.println("\n");

        }

        for (int row = 0; row < coord.length; row++) {

            for (int col = 0; col < Array[row].length; col++) {

                if (Array[row][col] % 3 == 0) coord[row][col] = -1;

            }

       }

        return coord;

    }

    public static void main(String[] args) {

        Scanner in = new Scanner(System. in );

        practice c = new practice();

        int[][] myArray;



        myArray = c.createArray(10, 10);



        c.print2DArray(myArray);

        c.createCoords(myArray);

    }

}
公共课堂实践
{
公共int[][]创建数组(int rSize,int cSize){
随机r=新随机();
int[][]数组=新的int[rSize][cSize];
for(int row=0;row
在检查
array
元素是否为3的倍数之前,您似乎正在打印
coords
数组,另外,您正在将
coords
初始化为all-1,因此将
coords
的元素更改为-1不会。。。没有什么。将
coords
初始化为所有0(这是默认值,因此您根本不需要初始化它,只需声明它),然后在检查
Array
后打印它

一些风格:首先,初始大写标识符通常为类保留,事实上在标准类库中已经有了
java.lang.reflect.Array
。改用
array
,或者更好的是,使用描述其用途的名称,而不是类型。其次,您已经有了一个
print2DArray
方法,为什么不在
createCoords
中使用它呢?最后,
coords
需要与
Array
(或将其重命名为:-)的大小相同,因此您应该这样声明它。如果更改传递给
createArray
的大小,还必须记住更改
coords