在这段代码中,我想创建一个3x3棋盘游戏,但屏幕上没有任何内容..(代码编译正确,但不显示输出)
我认为问题出在main
方法......无法弄明白......请帮助!
package games;
import games.board.*;
public class BoardGameTester {
/**
* @param args
*/
private static Board gb;
public static void main(String[] args) {
// TODO Auto-generated method stub
gb = new Board(3, 3);
}
}
这是一个board.java:
package games.board;
public class Board {
private Cell[][] cells;
public Board(int rows, int columns) {
cells = new Cell[rows][columns];
for( int r = 0; r < cells[0].length; r++ ) {
for (int c = 0; c < cells[1].length; c++) {
cells[r][c] = new Cell(r,c);
}
}
}
public void setCell(Mark mark, int row, int column) throws
IllegalArgumentException {
if (cells[row][column].getContent() == Mark.EMPTY)
cells[row][column].setContent(mark);
else throw new IllegalArgumentException("Player already there!");
}
public Cell getCell(int row, int column) {
return cells[row][column];
}
public String toString() {
StringBuilder str = new StringBuilder();
for( int r = 0; r < cells.length; r++ ) {
str.append("|");
for (int c = 0; c < cells[r].length; c++) {
switch(cells[r][c].getContent()) {
case NOUGHT:
str.append("O");
break;
case CROSS:
str.append("X");
break;
case YELLOW:
str.append("Y");
break;
case RED:
str.append("R");
break;
case BLUE:
str.append("B");
break;
case GREEN:
str.append("G");
break;
case MAGENTA:
str.append("M");
break;
case ORANGE:
str.append("M");
break;
default: //Empty
str.append("");
}
str.append("|");
}
str.append("\n");
}
return str.toString();
}
}
这是一个cell.java
package games.board;
public class Cell {
private Mark content;
private int row, column;
public Cell(int row, int column) {
this.row = row;
this.column = column;
content = Mark.EMPTY;
}
public Mark getContent() { return content; }
public void setContent(Mark content) { this.content = content; }
public int getRow() { return row; }
public int getColumn() { return column; }
}
这是mark.java
package games.board;
public enum Mark {
EMPTY, NOUGHT, CROSS, YELLOW, RED, BLUE, GREEN, MAGENTA, ORANGE
}
这是outcome.java
package games.board;
public enum Outcome {
PLAYER1_WIN, PLAYER2_WIN, CONTINUE, TIE
}
这是player.java
package games.board;
public enum Player {
FIRST,SECOND
}
答案 0 :(得分:0)
试试这个:
public static void main(String[] args) {
// TODO Auto-generated method stub
gb = new Board(3, 3);
System.out.println(gb.toString());
}
答案 1 :(得分:0)
您没有生成任何输出 打印板到控制台尝试:
public static void main(String[] args) {
// TODO Auto-generated method stub
gb = new Board(3, 3);
System.out.println(gb);
}
它将调用gb.toString()
方法
ps:如果您使用str.append("\n")
代替str.append("|")