Java 为什么玩家会倍增?
下面的示例代码分为两个类:World和Driver 世界级:Java 为什么玩家会倍增?,java,arrays,debugging,multidimensional-array,Java,Arrays,Debugging,Multidimensional Array,下面的示例代码分为两个类:World和Driver 世界级: public class World { private char[][] world2D; private int characterRow; private int characterColumn; private int score; private int collision = 0; public World(int width, int height){
public class World {
private char[][] world2D;
private int characterRow;
private int characterColumn;
private int score;
private int collision = 0;
public World(int width, int height){
world2D = new char[width][height];
characterColumn = 0;
characterRow = 0;
for(int i = 0; i < world2D.length; i++){
for(int j = 0; j < world2D[i].length; j++){
world2D[i][j] = '-';
}
}
world2D[characterRow][characterColumn] = 'P';
}
public void moveUp(){
world2D[characterRow][characterColumn] = '-';
if (characterRow > 0){
characterRow -= 1;
}
world2D[characterRow][characterColumn] = 'P';
score ++;
}
public void moveDown(){
world2D[characterRow][characterColumn] = '-';
if (characterRow < world2D.length){
characterRow += 1;
}
world2D[characterRow][characterColumn] = 'P';
score ++;
}
public void moveRight(){
world2D[characterRow][characterColumn] = '-';
if (characterColumn < (world2D[characterRow].length - 1)){
characterColumn += 1;
}
world2D[characterRow][characterColumn] = 'P';
score ++;
}
public void moveLeft(){
world2D[characterRow][characterColumn] = '-';
if (characterColumn > 0){
characterColumn -= 1;
}
world2D[characterRow][characterColumn] = 'P';
score ++;
}
public int getScore(){
return score;
}
public void displayWorld(){
for(int i = 0; i < world2D.length; i++){
for(int j = 0; j < world2D[i].length; j++){
System.out.print(world2D[i][j]);
}
System.out.println();
}
}
public int getCollision(){
return collision;
}
public int getCharacterRow(){
return characterRow;
}
public void addRock(){
int lastRow = world2D.length - 1;
int width = world2D[lastRow].length - 1;
int rockPosition = (int) (Math.random()*width);
if (world2D[lastRow][rockPosition] != 'P'){
world2D[lastRow][rockPosition] = '@';
}
if (world2D[lastRow][rockPosition] == 'P'){
collision = 1;
}
}
public void shiftWorldUp(){
for(int row = 0; row < world2D.length - 2; row++){
world2D[row] = world2D[row + 1];
}
//world2D[characterRow][characterColumn] = 'P';
}
}
当我尝试执行代码时,会出现一个奇怪的错误,数组的多个部分都用“p”字符填充,如下所示:
How tall should the world be?: 6
How wide should the world be?: 6
To move type 'up', 'down', 'left' or 'right
P-----
------
------
------
------
------
Next move?:
down
------
P-----
------
------
------
@-----
Next move?:
down
P-----
------
P-----
P-----
P-----
@---@-
Next move?:
我找不到这个虫子。这是一个有趣的,所以我希望有人能帮我找到它 当你执行shiftWorldUp时,你不会擦除p?我如何在shiftWorldUp方法中执行该操作?当世界向上移动时,玩家会发生什么情况?带有@符号或“rock”的行应该向上移动到下一行,最下面一行应该填充另一个“rock”
How tall should the world be?: 6
How wide should the world be?: 6
To move type 'up', 'down', 'left' or 'right
P-----
------
------
------
------
------
Next move?:
down
------
P-----
------
------
------
@-----
Next move?:
down
P-----
------
P-----
P-----
P-----
@---@-
Next move?: