2017-01-28 34 views
1

我有我的第二類(MazeSolver)不是從我的第一類(MazeInput)獲得的的cols值,因爲它應該是麻煩。這導致ArrayOutOfBoundsExceptionsNullPointerExceptions在我的MazeSolver中的drawMaze()方法。我明白爲什麼會出現這些錯誤,因爲缺乏對如何將字段傳遞給其他類的理解,我只是無能爲力。請問可愛的人請指出我要去哪裏?吸氣劑和setter方法和雞犬不寧

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


    } 
} 

回答

2

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(); 
+0

我想過這個問題,但在這一點上我會更需要的getter和setter方法? –

+0

@GregSmith據我可以告訴你不需要它們。 –

+0

這是問題的一半......我想用構造函數來做到這一點。然而,我需要獲得吸收者和制定者,並將他們作爲我的教授使用,並要求我們遵循他的「基本程序設計」。有沒有辦法用getter和setter來做到這一點? –