Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么这里不允许使用“void”类型?_Java_String_Tostring - Fatal编程技术网

Java 为什么这里不允许使用“void”类型?

Java 为什么这里不允许使用“void”类型?,java,string,tostring,Java,String,Tostring,我正在试着做一个扑克牌。 当我尝试编译时,我被告知此处不允许使用void type,它将我指向这一行: visualBoard = visualBoard + row + System.out.println(); 这里是整个方法的结尾 /** Makes a visual representation of the board. @param game board @return String visual of the board. */ @Override

我正在试着做一个扑克牌。 当我尝试编译时,我被告知此处不允许使用void type,它将我指向这一行:

  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是一个空类型,因为我在它里面放了一个字符串。有什么想法吗?

方法无效意味着没有返回值

System.out.println无效,因此不返回值


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

此行不正确:

visualBoard = visualBoard + row + System.out.println();
System.out.println不向其调用者返回字符串值,其返回类型为void。它只打印标准输出流上的任何字符串参数。这就是为什么你会犯这样的错误

如果要将空字符串连接到某个对象,可以这样做:

visualBoard = visualBoard + row + "";

你认为System.out.println是做什么的?为什么这样认为?我想它会跳到下一行。您可能正在寻找System.LineSeparator,您可以随时使用它。