吸气者和制定者以及完全混乱(Getters & setters & utter confusion)

编程入门 行业动态 更新时间:2024-10-24 23:14:54
吸气者和制定者以及完全混乱(Getters & setters & utter confusion)

我遇到了我的第二堂课(MazeSolver)没有从我的第一堂课(MazeInput)获得的值。 这是在MazeSolver的drawMaze()方法中导致ArrayOutOfBoundsExceptionsNullPointerExceptions 。 我理解为什么会发生这些错误,由于缺乏对如何将字段传递给其他类的理解,我只是无法解决它。 你们可爱的人请指出我哪里出错了吗?

public class LA2_MazeInput { private int rows; private int cols; public LA2_MazeInput() { this.rows = getNumRows(); this.cols = getNumCols(rows); } private int getNumRows() { int rows = 0; String input; input = JOptionPane.showInputDialog( "Please enter the number of rows (5-10) for the maze."); rows = Integer.parseInt(input); return rows; } private int getNumCols(int numRows) { int cols = 0; String input; input = JOptionPane.showInputDialog( "Please enter the number (5-10) of columns for the maze." + "\n(Value cannot be the same as the number of rows.)"); cols = Integer.parseInt(input); return cols; } public void initializeMazeSolver(/*MazeSolver solver*/) { LA2_MazeSolver ms = new LA2_MazeSolver(); ms.setNumRows(this.rows); ms.setNumCols(this.cols); } } public class LA2_MazeSolver { private int rows; private int cols; private String[][] maze; public LA2_MazeSolver() { this.maze = new String[rows][cols]; } public void setNumRows(int numRows) { this.rows = numRows; } public void setNumCols(int numCols) { this.cols = numCols; } public int getNumRows() { return this.rows; } public int getNumCols() { return this.cols; } public void drawMaze() { Random r = new Random(); maze[0][0] = "S"; maze[rows - 1][cols - 1] = "D"; int limit = ((rows * cols) / 3); for (int i = r.nextInt(limit) + 1; i < limit; i++) { maze[r.nextInt(rows - 1)][r.nextInt(cols - 1)] = "#"; } for (int i = 0; i < maze.length; i++) { for (int c = 0; c < maze[0].length; c++) { if (!(maze[i][c].matches("#")) && !(maze[i][c].matches("S")) && !(maze[i][c].matches("D"))) { maze[i][c] = Integer.toString(r.nextInt(100) + 1); } } } for (int i = 0; i < maze.length; i++) { for (int c = 0; c < maze[0].length; c++) { System.out.print(maze[i][c] + " "); } System.out.println(); } } }

I'm having trouble with my second class (MazeSolver) is not getting the values of rows and cols from my first class (MazeInput) as it should be. which is leading to ArrayOutOfBoundsExceptions and NullPointerExceptions in my drawMaze() method in MazeSolver. I understand why these errors are happening, I am just clueless how to address it due to a lack of understanding of how to pass fields to other classes. Could you lovely guys please point out where I am going wrong?

public class LA2_MazeInput { private int rows; private int cols; public LA2_MazeInput() { this.rows = getNumRows(); this.cols = getNumCols(rows); } private int getNumRows() { int rows = 0; String input; input = JOptionPane.showInputDialog( "Please enter the number of rows (5-10) for the maze."); rows = Integer.parseInt(input); return rows; } private int getNumCols(int numRows) { int cols = 0; String input; input = JOptionPane.showInputDialog( "Please enter the number (5-10) of columns for the maze." + "\n(Value cannot be the same as the number of rows.)"); cols = Integer.parseInt(input); return cols; } public void initializeMazeSolver(/*MazeSolver solver*/) { LA2_MazeSolver ms = new LA2_MazeSolver(); ms.setNumRows(this.rows); ms.setNumCols(this.cols); } } public class LA2_MazeSolver { private int rows; private int cols; private String[][] maze; public LA2_MazeSolver() { this.maze = new String[rows][cols]; } public void setNumRows(int numRows) { this.rows = numRows; } public void setNumCols(int numCols) { this.cols = numCols; } public int getNumRows() { return this.rows; } public int getNumCols() { return this.cols; } public void drawMaze() { Random r = new Random(); maze[0][0] = "S"; maze[rows - 1][cols - 1] = "D"; int limit = ((rows * cols) / 3); for (int i = r.nextInt(limit) + 1; i < limit; i++) { maze[r.nextInt(rows - 1)][r.nextInt(cols - 1)] = "#"; } for (int i = 0; i < maze.length; i++) { for (int c = 0; c < maze[0].length; c++) { if (!(maze[i][c].matches("#")) && !(maze[i][c].matches("S")) && !(maze[i][c].matches("D"))) { maze[i][c] = Integer.toString(r.nextInt(100) + 1); } } } for (int i = 0; i < maze.length; i++) { for (int c = 0; c < maze[0].length; c++) { System.out.print(maze[i][c] + " "); } System.out.println(); } } }

最满意答案

MazeSolver构造函数初始化大小为[0][0]二维数组。 之后,您尝试设置此时为时已晚的行和列。

LA2_MazeSolver ms = new LA2_MazeSolver(); // constructor of LA2_MazeSolver is called ms.setNumRows(this.rows); // does nothing for the array ms.setNumCols(this.cols); // does nothing for the array

为了解决这个问题,您可以将参数与构造函数一起传递。

public LA2_MazeSolver(int rows, int cols) { this.maze = new String[rows][cols]; this.rows = rows; // in case you want them to store this.cols = cols; // in case you want them to store }

反模式:

public LA2_MazeSolver() { } public void setNumRows(int numRows) { this.rows = numRows; } public void setNumCols(int numCols) { this.cols = numCols; } public void init(){ this.maze = new String[rows][cols]; }

会这样初始化它

LA2_MazeSolver ms = new LA2_MazeSolver(); ms.setNumRows(this.rows); ms.setNumCols(this.cols); ms.init();

Your MazeSolver constructor initializes your two dimensional array with the size of [0][0]. After that you try to set the rows and cols which it's too late at this point.

LA2_MazeSolver ms = new LA2_MazeSolver(); // constructor of LA2_MazeSolver is called ms.setNumRows(this.rows); // does nothing for the array ms.setNumCols(this.cols); // does nothing for the array

In order to fix this you could just pass the parameters along with the constructor.

public LA2_MazeSolver(int rows, int cols) { this.maze = new String[rows][cols]; this.rows = rows; // in case you want them to store this.cols = cols; // in case you want them to store }

The antipattern:

public LA2_MazeSolver() { } public void setNumRows(int numRows) { this.rows = numRows; } public void setNumCols(int numCols) { this.cols = numCols; } public void init(){ this.maze = new String[rows][cols]; }

Would init it like this

LA2_MazeSolver ms = new LA2_MazeSolver(); ms.setNumRows(this.rows); ms.setNumCols(this.cols); ms.init();

更多推荐

本文发布于:2023-04-27 15:35:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1327143.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:制定者   混乱   Getters   confusion   utter

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!