【问题标题】:Having trouble with 2d array objects二维数组对象有问题
【发布时间】:2015-10-13 03:22:48
【问题描述】:

所以在我进一步编写代码之前,我想确保它正确打印出来。不幸的是,我似乎找不到在屏幕上打印出整个数组的代码。在我的 GameBoard 类中,mines[][] 和 tileCount[][] 应该在数组中的每个 apot 中都有某种值,所以它们应该能够正确输出这些值吗?在我的主要方法中,我尝试了几种不同的方法,唯一一种接近 deeptoString 但只输出空值(所以我的想法是数组仍未初始化?)一个矩阵(用户输入可以将其更改为任何内容),如果调用 mines[][] 将值也作为矩阵输出。 总的来说,我正在寻找一种将二维数组输出到屏幕上的方法。

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





}

}

【问题讨论】:

    标签: java arrays object-oriented-analysis


    【解决方案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中的另一个错误:

    当 i 在最后一个循环中有一个数字 (Row - 1) 时,mines[i+x] 可能 mines[Row -1 + 1] 是 mines[Row] 并且会抛出异常(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]);
            }
    

    【讨论】:

    • 谢谢,这解决了我的问题。有没有办法只返回数组而不是在类中输出?
    • 如果你想通过GameBoard的toString方法输出,这是唯一的方法。但是你可以把`toString`方法放在GameClient中,将对象tileSize[0][0]作为参数传递给它,然后循环它的值输出。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-13
    • 2019-08-08
    • 2011-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多