我目前正在開發井字游戲,我遇到了一個可能有也可能沒有簡單修復的問題。我正在制作一個基于類和方法的井字游戲,我在多個具有多種方法的類中作業。
這是我的問題:
我有一個班級負責將所有者設定為板上的某些圖塊。
package game;
public class GameTile{
/**Stores a String representing the owner "X" or "O" of a GameTile
*/
protected String owner;
/**Constructs a GameTile, sets owner to null by default
*/
public GameTile (){
owner = null;
}
(我有更多的代碼,雖然我只展示了重要的東西,其余的代碼基本上宣告了“X”和“O”等......)
現在我正在學習上述課程并將其列印到游戲板課程中。
package game;
/**Class for Tic Tac Toe gameboard
*@author Put Name Here
*/
public class GameBoard{
protected static char[][] cells;
/**An array of 9 GameTiles representing the TicTacToe gameboard
*/
GameTile[] board;
/**Constructs an empty gameboard of 9 GameTiles and fills it with unowned tiles
*/
public GameBoard(){
board = new GameTile[9];
for (int i = 0; i < 9; i ) {
board[i] = new GameTile();
}
}
/**This will draw the current gameboard on the screen
*/
public void drawBoard(){
{
System.out.println();
System.out.println(" --- --- --- ");
System.out.println("| " board[0] " | " board[1] " | " board[2] " |");
System.out.println(" --- --- --- ");
System.out.println("| " board[3] " | " board[4] " | " board[5] " |");
System.out.println(" --- --- --- ");
System.out.println("| " board[6] " | " board[7] " | " board[8] " |");
System.out.println(" --- --- --- ");
}
}
}//end class
我的代碼中的所有內容似乎都運行良好,盡管當我從“board”(board[1]、board[2] 等...)列印任何內容時,輸出始終是這樣的:“game.GameTile@7de26db8”
有什么方法可以讓我的代碼從 GameTile() 列印字串值?
uj5u.com熱心網友回復:
對于您使用它的方式,請覆寫toString()GameTile 類中的方法。您現在看到的是該方法的 Object 類版本的結果。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/436215.html
上一篇:正文區域中沒有行時打開背景關系
