我目前正在尝试创建一个程序,它会吐出一个像这样的棋盘(它在实际的程序中看起来更好,只是为了编辑器不喜欢我使用“ - ”符号,所以我把它们放在引号):使用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