为什么变量在 Java 代码中只递增一次?
Why does variable only increment once in Java code?
我正在制作一个游戏,玩家 ('P') 从 [0][0] 处的二维破折号数组开始。提示玩家输入数组的大小,然后输入一个动作(左、右、上、下或退出)。输入该操作后,'P' 应该移动 1 个索引。
我正在处理我的 moveDown 方法,目前还没有别的。当我键入 "down" 时,它将递增到 theWorld[1][0],但如果我键入 "down",它不会再次递增(theWorld[2][0])。它只是停留在 [1][0]。
这个程序有两个 class,但是我的另一个 class 目前只有 main 方法和 world.userInput 方法,所以我不会包括那个。这是一项正在进行的工作,所以请原谅任何草率的代码、额外的 space 等。这是我目前所拥有的:
import java.util.Scanner;
class World {
private int characterRow;
private int characterColumn;
private char[][] theWorld;
char player = 'P';
int playerX = 0;
int playerY = 0;
String userAction;
boolean keepPlaying = true;
boolean outOfBounds;
public World() {
characterRow = 0;
characterColumn = 0;
}
public World(int height, int width) {
this.characterRow = height;
this.characterColumn = width;
}
public void setHeight(int height) {
characterRow = height;
}
public void setWidth(int width) {
characterColumn = width;
}
public void userInput(int height, int width, int x, int y) {
Scanner input = new Scanner(System.in);
System.out.print("Enter World width: ");
width = input.nextInt();
System.out.print("Enter World height: ");
height = input.nextInt();
displayWorld(height, width, x, y);
input.nextLine();
while(keepPlaying)
{
System.out.print("ACTION > ");
String action = input.nextLine();
while(!action.equalsIgnoreCase("exit"))
{
keepPlaying = true;
if(action.equalsIgnoreCase("down") && x > theWorld.length)
{
outOfBounds = true;
System.out.println("You cannot go out of bounds. Try again.");
}
else
break;
}
if(action.equalsIgnoreCase("down"))
{
moveDown(height, width, x, y, action);
}
}
public void moveDown(int height, int width, int x, int y, String action) {
if(x < theWorld.length-1)
{
x += 1;
}
displayWorld(height, width, x, y);
}
public void displayWorld(int height, int width, int x, int y) {
theWorld = new char[height][width];
for(height = 0; height < theWorld.length; height++)
{
for(width = 0; width < theWorld[height].length; width++)
{
theWorld[height][width] = '-';
theWorld[x][y] = player;
System.out.print(theWorld[height][width] + " ");
}
System.out.println();
}
}//end displayWorld
}//end class
我添加了一个 setPlayerX 方法,并将 moveDown 中的赋值更改为您在此处看到的内容。它现在有效,所以我想通了。
public void setPlayerX(int x) {
x = 1;
playerX = x;
}
public void moveDown(int height, int width, int x, int y) {
x++;
playerX = playerX + x;
displayWorld(height, width, x, y);
}
我正在制作一个游戏,玩家 ('P') 从 [0][0] 处的二维破折号数组开始。提示玩家输入数组的大小,然后输入一个动作(左、右、上、下或退出)。输入该操作后,'P' 应该移动 1 个索引。 我正在处理我的 moveDown 方法,目前还没有别的。当我键入 "down" 时,它将递增到 theWorld[1][0],但如果我键入 "down",它不会再次递增(theWorld[2][0])。它只是停留在 [1][0]。
这个程序有两个 class,但是我的另一个 class 目前只有 main 方法和 world.userInput 方法,所以我不会包括那个。这是一项正在进行的工作,所以请原谅任何草率的代码、额外的 space 等。这是我目前所拥有的:
import java.util.Scanner;
class World {
private int characterRow;
private int characterColumn;
private char[][] theWorld;
char player = 'P';
int playerX = 0;
int playerY = 0;
String userAction;
boolean keepPlaying = true;
boolean outOfBounds;
public World() {
characterRow = 0;
characterColumn = 0;
}
public World(int height, int width) {
this.characterRow = height;
this.characterColumn = width;
}
public void setHeight(int height) {
characterRow = height;
}
public void setWidth(int width) {
characterColumn = width;
}
public void userInput(int height, int width, int x, int y) {
Scanner input = new Scanner(System.in);
System.out.print("Enter World width: ");
width = input.nextInt();
System.out.print("Enter World height: ");
height = input.nextInt();
displayWorld(height, width, x, y);
input.nextLine();
while(keepPlaying)
{
System.out.print("ACTION > ");
String action = input.nextLine();
while(!action.equalsIgnoreCase("exit"))
{
keepPlaying = true;
if(action.equalsIgnoreCase("down") && x > theWorld.length)
{
outOfBounds = true;
System.out.println("You cannot go out of bounds. Try again.");
}
else
break;
}
if(action.equalsIgnoreCase("down"))
{
moveDown(height, width, x, y, action);
}
}
public void moveDown(int height, int width, int x, int y, String action) {
if(x < theWorld.length-1)
{
x += 1;
}
displayWorld(height, width, x, y);
}
public void displayWorld(int height, int width, int x, int y) {
theWorld = new char[height][width];
for(height = 0; height < theWorld.length; height++)
{
for(width = 0; width < theWorld[height].length; width++)
{
theWorld[height][width] = '-';
theWorld[x][y] = player;
System.out.print(theWorld[height][width] + " ");
}
System.out.println();
}
}//end displayWorld
}//end class
我添加了一个 setPlayerX 方法,并将 moveDown 中的赋值更改为您在此处看到的内容。它现在有效,所以我想通了。
public void setPlayerX(int x) {
x = 1;
playerX = x;
}
public void moveDown(int height, int width, int x, int y) {
x++;
playerX = playerX + x;
displayWorld(height, width, x, y);
}