为什么我的保安(“S”)与唐纳德(“D”)处于相同的位置。
地图应该像这样打印出来
[D----]
[- - - - -]
[- - - - -]
[- - S - -]
[- - P - -]
但它却像这样显示
[S----]
[- - - - -]
[- - - - -]
[- - - - -]
[- - P - -]
public class Main {
public static void main(String[] args) {
Map m = new Map();
Player p = new Player();
Donald d = new Donald();
Security s = new Security();
while(true) {
m.updateMap(p.row, p.col, p.character);
m.printMap();
m.updateMap(p.row, p.col, '-');
m.updateMap(d.row, d.col, d.character);
m.updateMap(s.row, s.col, s.character);
p.move();
}
}
}
public class Map {
char map[][];
Map() {
map = new char[5][5];
for(int i = 0; i<5; i++) {
for(int j = 0; j<5; j++) {
map[i][j] = '-';
}
}
}
void updateMap(int row, int col, char data) {
map[row][col] = data;
}
//prints map on the screen.
void printMap() {
for(int i = 0; i<5; i++) {
for (int j = 0; j<5; j++) {
System.out.print(map[i][j] + " ");
}
System.out.println();
}
}
}
public abstract class Position {
int row;
int col;
char character;
abstract void move();
}
public class Donald extends Position {
//Doanld Trump's Position on the Array is [0,0]
Donald() {
int row = 0;
int col = 0;
character = 'D';
}
void move() {
}
}
正如您在这里看到的,我将安全位置设置为 [3,2],但由于某种原因,它没有将其识别为 [3,2],并将安全位置设置为 Donald 坐的 [0,0]。
public class Security extends Position {
Security() {
int row = 3;
int col = 2;
character = 'S';
}
void move() {
}
}
倚天杖
相关分类