【问题标题】:How to create a 2d grid array with information from another如何使用来自另一个的信息创建二维网格数组
【发布时间】:2015-11-16 01:47:38
【问题描述】:

我正在尝试打印一个带有随机数的 2d 网格和另一个相同大小的网格,其中 -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);

    }

}

【问题讨论】:

    标签: java arrays methods


    【解决方案1】:

    看起来您在检查 Array 元素是否为 3 的倍数之前打印了 coords 数组,而且您正在将 coords 初始化为全 -1,因此更改了 @987654324 的元素@ to -1 不会……什么都没有。将coords初始化为全0(这是默认的,所以你根本不需要初始化它,只需声明它)然后在检查Array后打印它。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-16
      • 2021-08-01
      相关资源
      最近更新 更多