有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在java中为2d数组调用方法

我正在为一门编程课做一项作业,内容涉及骑士之旅的启发。目前,我有一种方法,可以用“棋盘”对象填充8x8棋盘阵列,这些对象知道棋盘上该空间的“可访问性”,以及该棋盘是否以前实际访问过。但是,当我创建8x8阵列并尝试调用此阵列的“填充”方法时,我收到一个错误,告诉我所使用的包中不存在该阵列。。。我做错了什么?它不应该像声明数组和调用方法那样简单吗

Board [][] chessboard = new Board [8][8];
chessboard.fill();

还是我的语法错了?这里是我的代码,用于创建线路板对象、可访问性矩阵,然后将这些可访问性值复制到8x8阵列上的每个线路板对象,以供参考。谢谢

public class Board {

/*
 * Initialize array that emulates chessboard. Will be 8x8, each space will 
 * contain the number of squares from which that space can be reached. The 
 * knight will start at a new space each tour and choose each move based on
 * the "accessability" of each square within its "move pool". The knight 
 * will move to the square with least accesibility each time. When the 
 * Knight is moved to a square, this square will be marked as visited so 
 * that it cannot be visited again. Also, any space that could have been 
 * moved to but was not will have its accesability reduced by 1.     
 */

private boolean visited;
private int accessValue;
private Board [][] chess = new Board[8][8];

public Board(int acessability, boolean beenVisited)
{
    visited = beenVisited;
    accessValue = acessability;  
}

int [][] accessMatrix = {{2,3,4,4,4,4,3,2},
                        { 3,4,6,6,6,6,4,3 },
                        { 4,6,8,8,8,8,6,4 },
                        { 4,6,8,8,8,8,6,4 },
                        { 4,6,8,8,8,8,6,4 },
            { 4,6,8,8,8,8,6,4 },
            { 3,4,6,6,6,6,4,3 },
            { 2,3,4,4,4,4,3,2}};





public void fill()
{

for (int i = 0 ; i < accessMatrix.length ; i++)
{
    chess[0][i].changeAccess(accessMatrix[0][i]);
}
for (int i = 0 ; i < accessMatrix.length ; i++)
{
    chess[1][i].changeAccess(accessMatrix[1][i]);
}
for (int i = 0 ; i < accessMatrix.length ; i++)
{
    chess[2][i].changeAccess(accessMatrix[2][i]);
}
for (int i = 0 ; i < accessMatrix.length ; i++)
{
    chess[3][i].changeAccess(accessMatrix[3][i]);
}
for (int i = 0 ; i < accessMatrix.length ; i++)
{
    chess[4][i].changeAccess(accessMatrix[4][i]);
}
for (int i = 0 ; i < accessMatrix.length ; i++)
{
    chess[5][i].changeAccess(accessMatrix[5][i]);
}
for (int i = 0 ; i < accessMatrix.length ; i++)
{
    chess[6][i].changeAccess(accessMatrix[6][i]);
}
for (int i = 0 ; i < accessMatrix.length ; i++)
{
    chess[7][i].changeAccess(accessMatrix[7][i]);
}

}   




public int getAccess()
{
return accessValue;
}

public int changeAccess(int newAccess)
{
int accessNew;
accessNew = newAccess;
return accessNew;    
}

共 (1) 个答案

  1. # 1 楼答案

    在此代码中:

    Board [] chessboard = new Board [8][8];
    

    aBoard[8][8]的类型是Board[][]

    因此,应该这样编写,以编译:

    Board [][] chessboard = new Board [8][8];
    

    (然后您必须在数组中创建每个Board()对象,我将此作为练习留给您)

    在此代码中:

    chessboard.fill();
    

    您正在调用Board的方法。只能在线路板对象上执行此操作,而不能在阵列上执行此操作。如果要对阵列中的每个板对象调用此方法,必须执行以下操作:

    for (int i = 0; i <8; i ++) {
        for (int j = 0; j < 8; j ++){
             chessboard[i][j].fill();
        }
    }
    

    但我觉得还有更多,因为有一些困惑。我认为,棋盘是一块棋盘,而不是一组棋盘。您可能想要创建一个棋盘并填充一次。正当然后简单地这样做:

     Board chessboard = new Board();
     chessboard.fill();