为什么我得到 'void' type not allowed here?

Why am I getting 'void' type not allowed here?

我正在尝试为纸牌创建一个 toString。 当我尝试编译时,我被告知 "void type not allowed here" 并且它指向这一行:

  visualBoard = visualBoard + row + System.out.println();

整个方法到此结束

  /**
  Makes a visual representation of the board.
  @param game board
  @return String visual of the board.
  */
  @Override
  public String toString()
  {
     String visualBoard = "";
     //For loop for each spot on board
     for(int r = 0; r < 3; r++){
        String row = "";
        for(int c = 0; c < 4; c++){
           //Adding the board square to the current string.
           BoardSquare current = board.get(r).get(c);
           row += current.getCard(r,c).toString() + "_";
        }
        visualBoard = visualBoard + row + System.out.println();

     }
     return visualBoard;
  }

我真的很困惑为什么他们告诉我 row 是 void 类型,因为我确实在其中放了一个字符串。有什么想法吗?

方法上的

void 表示没有 return 值。

System.out.println 无效,因此 return 不是一个值。

因此 + 没有任何要添加的内容,编译器会报错。

此行不正确:

visualBoard = visualBoard + row + System.out.println();

System.out.println() 不会 return 给它的调用者一个字符串值,它的 return 类型是空的。它只是在 stdout 流上打印任何字符串参数。这就是您收到该错误的原因。

如果您想将空字符串连接到某个内容,可以这样做:

visualBoard = visualBoard + row + "";