Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.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
我能';是否打印包含对象和我的类的数组网格?netbeansw/Java_Java_Arrays_Class_Object_Maze - Fatal编程技术网

我能';是否打印包含对象和我的类的数组网格?netbeansw/Java

我能';是否打印包含对象和我的类的数组网格?netbeansw/Java,java,arrays,class,object,maze,Java,Arrays,Class,Object,Maze,我正在用扫描仪确定的数量做一个网格。我不断地犯错误,我不知道为什么。下面是我试图制作的网格的代码,我还将在下面包括迷宫/网格的对象和类 public static void mazeSetup() { System.out.println("How many rows and columns do you want? I would\n" + "suggest 10

我正在用扫描仪确定的数量做一个网格。我不断地犯错误,我不知道为什么。下面是我试图制作的网格的代码,我还将在下面包括迷宫/网格的对象和类

public static void mazeSetup() {                                        
    System.out.println("How many rows and columns do you want? I would\n"
                     + "suggest 10 minimum and 20 maximum.");
    boolean mazeselect = true;
    while(mazeselect) {
    maze.columns = sc.nextInt();
    maze.rows = maze.columns;
    if (maze.rows > 30 || maze.rows < 10) {
        System.out.println("Make sure that you make it within 10-30 rows.");
    } else {
        mazeselect = false;
    }
  }
    mazeBuild();
}

public static void mazeBuild() {
    for(int x = 0; x < maze.rows; x++) {
        for(int y = 0; y < maze.columns; y++) {
            maze.maze[x][y]= ".";
            System.out.print(maze.maze[x][y]);
    }
        System.out.println();
}
characterPlacement();
以及迷宫/网格的构造函数类

public class Maze {

    String maze[][];
    int rows;
    int columns;
    int xStart;
    int yStart;

public Maze(String xMaze[][], int xRows, int xColumns, int xxStart, int xyStart) {     
        maze = xMaze;
        rows = xRows;
        columns = xColumns;
        xStart = xxStart;
        yStart = xyStart;
    }
    public String[][] maze() {
        return maze;
    }
    public int rows() {
        return rows;
    }
    public int columns() {
        return columns;
    }
    public int xStart() {
        return xStart;
    }
    public int yStart() {
        return yStart;
    }
}

任何帮助都将不胜感激。非常感谢D


注意:在控制台中运行之前不会发生任何错误。

您的
字符串迷宫[][]
null
,原因如下:

static Maze maze = new Maze(null,0,0,0,0); // notice that null
您试图在调用
mazeBuild()
时将值放入其中。您应该初始化它或传递数组,而不是
null
。您可以在
mazeBuild()的开头执行此操作


嘿,我还有一个问题。如果我有一个对象,比如说它需要一个int,string[]],我该如何把这个字符串放在参数中?当你说“object that required a int,string[][]”时,你的意思是它的构造函数需要这两个参数吗?是的,当我调用它时,我不知道如何调用任何一个string参数,这就是为什么我把它设为null。有什么办法吗?你可以做我答案中的第二个选项,也可以直接像这样写,
maze=newmaze(newstring[rows][columns],other,params)
static Maze maze = new Maze(null,0,0,0,0); // notice that null
public static void mazeBuild() {
    maze.maze = new String[maze.rows][maze.columns]; // <-- this one!

    for(int x = 0; x < maze.rows; x++) { // <-- this loop tries to
        for(int y = 0; y < maze.columns; y++) { // put values in your
            maze.maze[x][y]= ".";               // maze.maze (2D String array)
            System.out.print(maze.maze[x][y]);
    }
    System.out.println();
}
String[][] mazeArray = new String[maze.rows][maze.columns];
maze = new Maze(mazeArray, maze.rows, maze.columns, 0, 0);