我不知道为什么我得到.class'预期'错误

use*_*515 -3 java

public String toString()
{
    String s = "";
    s += String.format("%02d" ,board[][] + " ");
    s += "/n" +"/n" + "The knight made" + (moves) + "moves";
    return s;
}
Run Code Online (Sandbox Code Playgroud)

我不知道为什么,但我一直在说错误.class 'expected'.这是什么意思,我该如何解决?

das*_*ght 7

您的代码有三个错误和效率低下.

第一个错误是您尝试在单个语句中打印2D数组.你做不到 - 你需要两个嵌套循环:

for (int r = 0; r != maxRow ; r++) {
    for (int c = 0; c != maxCol ; c++) {
         // Do the construction of the string here.
         // Refer to board[r][c] instead of board[][]
    }
}
Run Code Online (Sandbox Code Playgroud)

第二个错误是"/n"不是换行符:你需要一个反斜杠.

第三个错误是您尝试使用以下%d格式打印字符串:您应该使用board[r][c]不使用+ " ",并将空格放在格式字符串中:

String.format("%02d " ,board[r][c])
Run Code Online (Sandbox Code Playgroud)

效率低下的是您正在使用循环中调用的连接构造结果字符串.这会创建大量临时对象.你应该StringBuilder上课.