简体   繁体   English

如何使用其他信息创建2D网格阵列

[英]How to create a 2d grid array with information from another

I'm trying to print a 2d grid with random numbers and another equally sized grid with -1s in place of the numbers that are evenly divisible by 3. I finished pretty much everything, but the second grid is only printing -1s. 我正在尝试打印一个带有随机数的2d网格,以及另一个带有-1s的相等大小的网格,以代替被3整除的数字。我几乎完成了所有工作,但是第二个网格仅打印了-1s。 What am I doing wrong? 我究竟做错了什么? How can I get the second grid to only print -1s where the numbers are evenly divisible by 3? 如何获得第二个网格仅打印数字被3整除的-1s? Thanks! 谢谢!

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

    }

}

It looks like you're printing the coords array before you're checking for Array elements being a multiple of 3, plus you're initializing coords to all -1 so changing an element of coords to -1 does... nothing. 看起来您在检查Array元素是否为3的倍数之前正在打印coords数组,而且您正在将coords初始化为全部-1,因此将coords的元素更改为-1不会执行任何操作。 Initialize coords to all 0's (which is the default, so you don't need to initialize it at all, just declare it) then print it after checking Array . coords初始化为全0(这是默认值,因此您根本不需要初始化它,只需声明它),然后在检查Array之后将其打印出来。

A few style things: First, initial uppercase identifiers are generally reserved for classes, in fact there's already a java.lang.reflect.Array in the standard class library. 一些样式方面的事情:首先,初始大写标识符通常是为类保留的,实际上,标准类库中已经有一个java.lang.reflect.Array Use array instead, or better yet, use a name that describes it's use , not it's type. 使用array来替代,或者更好的是,使用能够描述它的使用 ,而不是它的类型的名称。 Second, you've already got a print2DArray method, why not use it in createCoords ? 其次,您已经有了一个print2DArray方法,为什么不在createCoords使用它呢? Finally, coords needs to be the same size as Array (or whatever you rename it to :-) ), so you should declare it as such. 最后, coords必须与Array大小相同(或将其重命名为:-)),因此应这样声明。 If you change the size you pass to createArray , you have to remember to change coords as well. 如果更改传递给createArray的大小,则还必须记住还要更改coords

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM