2015-10-13 91 views
0

因此,在進一步編寫代碼之前,我想確保它能正確打印出來。不幸的是,我似乎無法找到代碼在屏幕上輸出整個數組。在我的GameBoard類中,mines [] []和tileCount [] []應該在數組中的每個apot中都有某種值,所以他們應該能夠正確輸出這些值?在我的主要方法中,我嘗試了幾種不同的方法,並且只有一個關閉了deeptoString,但只輸出了空值(所以我的想法是該數組還沒有被初始化?)我正在尋找並從char中取出一個矩陣(用戶輸入可以將它改變爲任何內容),並且如果調用mines [] [],則也將這些值作爲矩陣。總的來說,我正在尋找一種方法將2d數組輸出到屏幕上。在使用2d數組對象時遇到問題

import java.util.Random; 
    import java.util.Scanner; 

public class GameBoard 
{ 
    private int[][] mines; 
    private char[][] tileCount; 
    private int Row, Column; 
    Random random = new Random(); 
    Scanner input = new Scanner(System.in); 

public GameBoard(int Row, int Column) 
{ 
    Row = Row; 
    Column = Column; 
    mines = new int[Row][Column]; 
    tileCount = new char[Row][Column];   
    startMines(); 
    randomMines(); 
    fillTips(); 
    startBoard(); 

} 
public void startMines(){ 
    for(int i=0 ; i<mines.length ; i++) 
     for(int j=0 ; j<mines[0].length ; j++) 
      mines[i][j]=0; 

} 

public void randomMines() 
{ 
    boolean raffled; 
    int x = Row * Column; 
    int tempRow, tempColumn; 

    for(int i=0 ; i<x ; i++) 
    { 
     tempRow = (int)(Math.random() + 1.0); 
     tempColumn = (int)(Math.random() + 1.0); 

     if(mines[tempRow][tempColumn] == 0) 
     { 
      mines[tempRow][tempColumn] = 11; 
     } 

    } 
} 

public void fillTips(){ 
    for(int i=1 ; i < Row ; i++) 
     for(int j=1 ; j < Column ; j++){ 

      for(int x=-1 ; x<=1 ; x++) 
       for(int y=-1 ; y<=1 ; y++) 
        if(mines[i][j] != -1) 
         if(mines[i+x][j+y] == -1) 
          mines[i][j]++; 

     } 

} 

public void startBoard(){ 
    for(int i=1 ; i<mines.length ; i++) 
     for(int j=1 ; j<mines.length ; j++) 
      tileCount[i][j]= '_'; 

} 

public String toString() 
{ 
    System.out.println("\n  Row"); 
    System.out.print("Row: " + Row + "\nColumn: " + Column); 
    for(int i = 1 ; i < Row ; i++){ 
     System.out.print("  "+Row + " "); 

     for(int j = 1 ; j < Column ; j++){ 
      return " "+ tileCount[i][j]; 
     } 

     System.out.println(); 
    } 

    return "\n   1 2 3 4 5 6 7 8\n      Columns"; 

} 

}

import java.util.Scanner; 
import java.util.Arrays; 
public class GameClient { 
int grid; 

public static void main(String args[]) { 
    System.out.println("Welcome to the game minesweeper, I hope you enjoy your stay."); 
    System.out.println("We will begin by asking how large you would like the game."); 
    System.out.println("---------------------------------------------------------------"); 
    System.out.println("Input two values please: "); 
    Scanner grid = new Scanner(System.in); 
    int grid1 = grid.nextInt(); 
    int grid2 = grid.nextInt(); 
    double mineCount = ((grid1*grid2)*.25); 
    System.out.println("Enter a number of mines, please 25% less than the total tiles which is " + mineCount); 
    Scanner mineCounts = new Scanner(System.in); 
    mineCount = mineCounts.nextInt(); 
    GameBoard[][] tileSize = new GameBoard[grid1][grid2]; 
    tileSize[0][0] = new GameBoard(grid1, grid2);  


    System.out.println(tileSize.toString()); 

    System.out.println(tileSize[0][0]); 
    System.out.println(Arrays.deepToString(tileSize)); 





} 

}

回答

1

因爲當tileSize [0] [0]被初始化時發生異常。

看我的日誌:

Welcome to the game minesweeper, I hope you enjoy your stay. 
We will begin by asking how large you would like the game. 
--------------------------------------------------------------- 
Input two values please: 
100 
10 
Enter a number of mines, please 25% less than the total tiles which is 250.0 
3 
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10 
    at GameBoard.startBoard(GameBoard.java:67) 
    at GameBoard.<init>(GameBoard.java:21) 
    at GameClient.main(GameClient.java:19) 

。在你的代碼中的錯誤:

public GameBoard(int Row, int Column) 
{ 
    Row = Row; 
    Column = Column; 
    ....... 
} 

這應該是:

public GameBoard(int Row, int Column) 
{ 
    this.Row = Row; 
    this.Column = Column; 
    ..... 
} 

的方法fillTips另一個錯誤:

wh。

wh我在最後一個循環中有一個數字(行 - 1),礦井[i + x]可能是礦井[行-1 + 1],這是礦井[行],並且會拋出異常(java.lang.ArrayIndexOutOfBoundsException)。

另一個錯誤...:

for(int j = 1 ; j < Column ; j++){ 
      return " "+ tileCount[i][j]; 
     } 

應該是:

for(int j = 1 ; j < Column ; j++){ 
      System.out.print(" "+ tileCount[i][j]); 
     } 
+0

謝謝,這解決了我的問題。沒有一種方法可以返回數組而不是在類中輸出嗎? – Consultion

+0

如果你想通過GameBoard的'toString'方法輸出它,這是唯一的方法。但是,您可以將'toString'方法放入GameClient中,將對象tileSize [0] [0]作爲參數傳遞給它並循環其值以輸出。 – tianwei

相關問題