我目前正在嘗試創建一個程序,它會吐出一個像這樣的棋盤(它在實際的程序中看起來更好,只是爲了編輯器不喜歡我使用「 - 」符號,所以我把它們放在引號):使用2D arraylist創建一個棋盤和棋子
-----------------
| | | | |K| | | |
-----------------
| |P| | | |P| | |
-----------------
| | | | | | | | |
-----------------
| | | | | | | | |
-----------------
| | | | | | | | |
-----------------
| | | | | | | | |
-----------------
| | | | | |N| | |
-----------------
| | | | |K| | | |
-----------------
我使用兩種方法,一個showBoard方法和addPiece方法。我目前堅持使用addPiece方法,並且我試圖讓它使用三個輸入:行int,列int和字符串名稱(例如K只是K)。但是,我無法使用addPiece方法將片段放到我想要的位置,或者甚至完全沒有問題。這是我到目前爲止有:
public class ChessBoard {
public static String[][] board = new String[8][8];
public static int row = 0;
public static int col = 0;
public static void addPiece(int x, int y, String r){
board[x][y] = new String(r);
}
public static void showBoard(){
for (row = 0; row < board.length; row++)
{
System.out.println("");
System.out.println("---------------");
for(col = 0; col < board[row].length; col++)
{
System.out.print("| ");
}
}
System.out.println("");
System.out.println("---------------");
}
public static void main(String[] args) {
System.out.println(board.length);
showBoard();
addPiece(1,2,"R");
}
}
我知道它是與我寫我addpiece法的方式,但我仍然爲編寫方法應該怎麼樣的困惑,那就是我最好的嘗試(不起作用)。有沒有人有什麼建議?謝謝!
如果您將帖子的部分標記爲代碼,編輯器將接受它們,而不管它們包含的任何格式化符號。當你showBoard()第一次顯示板時,你需要輸入 – 2013-04-10 17:27:50
。你添加你的作品。請注意,你之後沒有再次顯示板子? – 2013-04-10 17:28:01
此外,你可以簡單地寫'board [x] [y] = r;' – 2013-04-10 17:29:31