我必须以播放器作为对象创建一个Battleship程序,然后将Array Board作为字段的一部分。我在代码中的某个地方有一个小错误,我似乎无法确定它究竟是什么。我有一个main方法和一个定义我的方法的类。我收到一条错误消息,说它无法在" a.setBoard()"找到符号。和" b.setBoard()"
public class Player_p2 implements Player{
//fields
char [][] Board1 = new char [11][10];
char [][] Board2 = new char[11][10];
//methods
public void setBoard () {
this.Board1 = Board1;
this.Board2 = Board2;
int row = Board1.length;
int col = Board1[0].length;
char a = '~';
for (int k = 1; k < row; k++) {
for (int l = 0; l < col; l++) {
Board1[k][l] = a;
}
}
int row1 = Board1.length;
int col1 = Board1[0].length;
char b = '~';
for (int m = 1; m < row1; m++) {
for (int n = 0; n < col1; n++) {
Board1[m][n] = b;
}
}
Board1[0][0] = '0';
Board1[0][1] = '1';
Board1[0][2] = '2';
Board1[0][3] = '3';
Board1[0][4] = '4';
Board1[0][5] = '5';
Board1[0][6] = '6';
Board1[0][7] = '7';
Board1[0][8] = '8';
Board1[0][9] = '9';
Board2[0][0] = '0';
Board2[0][1] = '1';
Board2[0][2] = '2';
Board2[0][3] = '3';
Board2[0][4] = '4';
Board2[0][5] = '5';
Board2[0][6] = '6';
Board2[0][7] = '7';
Board2[0][8] = '8';
Board2[0][9] = '9';
}
public void printBoard() {
this.Board2 = Board2;
int row = Board2.length;
int col = Board2[0].length;
for (int k = 0; k < row; k++) {
System.out.print(k + " ");
for (int l = 0; l < col; l++) {
System.out.print(Board2[k][l] + " ");
}
System.out.println();
}
}
}
public class Battleship {
public static void main(String[] args) {
Player a = new Player_p2();
Player b = new Player_p2();
//initializes the empty boards
a.setBoard();
b.setBoard();
a.printBoard();
b.printBoard();
}
}
public interface Player{
public void printBoard();
}
答案 0 :(得分:2)
未示出的基类或接口Player
可能未指定方法setBoard()
。
答案 1 :(得分:2)
更改
Player a = new Player_p2();
要
Player_p2 a = new Player_p2()
答案 2 :(得分:1)
只需将Player更改为Player_p2
即可