简体   繁体   English

2D阵列无法打印

[英]2D array not printing

I'm trying to make a 2D array that essentially acts as a room/grid - like a top-down view. 我正在尝试制作一个本质上充当房间/网格的2D数组-像自顶向下的视图一样。

This code creates an array of dimensions row x col based on user input. 此代码根据用户输入创建尺寸为行x col的数组。 I then have it initialize every cell with a "." 然后,我用“。”初始化每个单元格。 to represent a space. 代表一个空间。

My question is, why, when I use the printRoom() method does it not print anything out at all? 我的问题是,为什么当我使用printRoom()方法时,它根本不打印任何内容? The double nested for-loops are running through the array, but nothing gets printed. 双嵌套的for循环正在数组中运行,但是没有输出任何内容。

I know it has to do with the user input, because I tried just initializing with row = 5 and col = 5, and it printed just fine. 我知道这与用户输入有关,因为我尝试仅使用row = 5和col = 5进行初始化,并且打印效果很好。

Thank you for your help. 谢谢您的帮助。

Here is the Room Class: 这是房间等级:

public class Room
{
    int row, col;   
    String[][] arr = new String[row][col];

    //Room Constructor
    public Room(int row, int col)
    {
        this.row = row;
        this.col = col;              
    }

    //Fills the Room with "."
    public void fillRoom()
    {
        for (int i = 0; i < arr.length; i++)
        {
            for (int j = 0; j < arr[i].length; j++)
                arr[i][j] = ".";
        }
    }

    //Prints the entire room/grid
    public void printRoom()
    {
        for (int i = 0; i < arr.length; i++)
        {
            for (int j = 0; j < arr[i].length; j++)
                System.out.print(arr[i][j]);

            System.out.println();
        }
    }
}

Here is my RoomTest class which contains main: 这是我的RoomTest类,其中包含main:

import java.util.*;

public class RoomTest
{
    public static void main (String[] args)
    {
        Scanner scan = new Scanner(System.in);
        int row, col;

        System.out.println ("Enter room dimensions (row col)");
        row = scan.nextInt();
        col = scan.nextInt();        

        Room room1 = new Room(row,col);
        room1.fillRoom();
        room1.printRoom();
    }
}

Here is the output when I run and enter user input: 这是我运行并输入用户输入时的输出:

Enter room dimensions (col row) 输入房间尺寸(行列)

4 6 4 6

When you initialize your array, row and col are zero, so it is a 0x0 array. 初始化数组时, rowcol为零,因此它是一个0x0数组。

As such, the guard condition in for (int i = 0; i < arr.length; i++) is immediately false, and so never executes. 这样, for (int i = 0; i < arr.length; i++)的保护条件立即为false,因此从不执行。

You need to move the array initialization into the constructor: 您需要将数组初始化移到构造函数中:

arr = new String[row][col];

Try to add length of the 2D array here because before are initialized with zero: 尝试在此处添加2D数组的长度,因为before被初始化为零:

  int row= 4, col= 6; 
  String[][] arr = new String[row][col];

Or you can initialize it in Constructor like this: 或者,您可以像下面这样在Constructor中对其进行初始化:

  String[][] arr;
//Room Constructor
public Room(int row, int col)
   {
   arr = new String[row][col];
    this.row = row;
    this.col = col;              
    }

And in your main method initialize the row and col : 然后在您的main方法中初始化rowcol

 int row= 4, col= 6;

To solve the problem create the array at the constructor of Room object: 要解决此问题,请在Room对象的构造函数中创建数组:

int row, col;   
String[][] arr = null

//Room Constructor
public Room(int row, int col)
{
    this.row = row;
    this.col = col;
    arr = new String[row][col];
}

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

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