Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么变量在Java代码中只增加一次?_Java_Arrays_Loops_Java.util.scanner_Post Increment - Fatal编程技术网

为什么变量在Java代码中只增加一次?

为什么变量在Java代码中只增加一次?,java,arrays,loops,java.util.scanner,post-increment,Java,Arrays,Loops,Java.util.scanner,Post Increment,我正在制作一个游戏,玩家('p')从[0][0]的2d破折号数组开始。提示玩家输入阵列的大小,然后输入动作(左、右、上、下或退出)。当输入该操作时,“P”应该移动1个索引。 我正在研究我的向下移动方法,其他什么都没有。当我键入“down”时,它将增加到world[1][0],但如果我键入“down”,它将不会再次增加(world[2][0])。它只是停留在[1][0] 这个程序有两个类,但是我的另一个类现在只有main方法和world.userInput方法,所以我不包括它们。这是一项正在进行的

我正在制作一个游戏,玩家('p')从[0][0]的2d破折号数组开始。提示玩家输入阵列的大小,然后输入动作(左、右、上、下或退出)。当输入该操作时,“P”应该移动1个索引。 我正在研究我的向下移动方法,其他什么都没有。当我键入“down”时,它将增加到world[1][0],但如果我键入“down”,它将不会再次增加(world[2][0])。它只是停留在[1][0]

这个程序有两个类,但是我的另一个类现在只有main方法和world.userInput方法,所以我不包括它们。这是一项正在进行的工作,请原谅任何草率的代码、额外的空间等。以下是我到目前为止的情况:

    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
import java.util.Scanner;
阶级世界{
私有int字符行;
私有int字符列;
私有字符[][]世界;
charplayer='P';
int playerX=0;
int playerY=0;
字符串用户操作;
布尔值keepPlaying=true;
布尔出边界;
公共世界(){
characterRow=0;
characterColumn=0;
}
公共世界(整数高度、整数宽度){
this.characterRow=高度;
this.characterColumn=宽度;
}
公共空间设置高度(内部高度){
characterRow=高度;
}
公共void setWidth(int-width){
characterColumn=宽度;
}
公共void用户输入(整数高度、整数宽度、整数x、整数y){
扫描仪输入=新扫描仪(System.in);
System.out.print(“输入世界宽度:”);
宽度=input.nextInt();
System.out.print(“输入世界高度:”);
高度=输入。nextInt();
显示世界(高度、宽度、x、y);
input.nextLine();
同时(继续播放)
{
系统输出打印(“操作>”);
String action=input.nextLine();
而(!action.equalsIgnoreCase(“退出”))
{
持续播放=正确;
if(action.equalsIgnoreCase(“down”)和&x>theWorld.length)
{
边界外=真;
System.out.println(“您不能越界,再试一次”);
}
其他的
打破
}
if(action.equalsIgnoreCase(“down”))
{
向下移动(高度、宽度、x、y、动作);
}
}
公共无效向下移动(整数高度、整数宽度、整数x、整数y、字符串动作){
如果(x
我添加了一个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);

}

你确定
theWorld.length
>2吗?是的。我现在用5表示高度和宽度。只是一个小数字来完成程序。它会递增一次,然后我再次键入“down”,它会重新打印数组,而“P”仍然在[1][0],无论我做了多少次。您是否尝试过在
向下移动中打印它,只是为了再次检查?这就是为什么我有显示世界(高度、宽度、x、y)方法。也许我的逻辑是错误的。它第一次重印很好,只是在任何后续操作之后都没有重印。为什么这个问题会被否决?我看到初学者在挣扎时的立场。不管怎样,我找到了答案,所以我会发布我的答案。