【问题标题】:Reading Zero For Value of all Places in Array为数组中所有位置的值读取零
【发布时间】:2014-08-02 15:58:54
【问题描述】:

我正在使用 Swing 用 Ja​​va 制作游戏。我正在使用 2D 数组来存储我的世界。我有一个文本命令系统,当它从数组中调用一个变量时,它在除 0,0 之外的每个位置返回 0。这是我的代码:

生成数组的类:

package finalproject;

public class WorldGen {

public int length = 20;
public int width = 20;
public double[][] world = new double[length][width];

public WorldGen(int seed) {

    worldBase(seed);

}

public void worldBase(int seed) {

    if(seed == 1) {

        world[0][0] = 1;

    } else if (seed == 2) {

        world[0][0] = 2;

    } else if (seed == 3) {

        world[0][0] = 3;

    } else if (seed == 4) {

        world[0][0] = 4;

    } else if (seed == 5) {

        world[0][0] = 5;

    } else if (seed == 0) {

        world[0][0] = 1 + (Math.floor(Math.random() * 5));

    }

    worldSmooth(0, 0);

}


public void worldSmooth(int posX, int posY) {

    double tempChunkType;

    for(int x = 1; x < length; x ++) {

        for(int y = 1; y < width; y ++) {

            if(world[posX][posY] == 1) {

                tempChunkType = 1 + (Math.floor(Math.random() * 10));
                if(tempChunkType == 1 || tempChunkType == 2 || tempChunkType == 3 || tempChunkType == 4 || tempChunkType == 5) {

                    world[x][y] = 1;

                } else if(tempChunkType == 6 || tempChunkType == 7 || tempChunkType == 8) {

                    world[x][y] = 2;

                } else if(tempChunkType == 9) {

                    world[x][y] = 3;

                } else if(tempChunkType == 10) {

                    world[x][y] = 4;

                }

            } else if(world[posX][posY] == 2) {

                tempChunkType = 1 + (Math.floor(Math.random() * 10));
                if(tempChunkType == 1 || tempChunkType == 2 || tempChunkType == 3 || tempChunkType == 4 || tempChunkType == 5) {

                    world[x][y] = 2;

                } else if(tempChunkType == 6 || tempChunkType == 7 || tempChunkType == 8) {

                    world[x][y] = 1;

                } else if(tempChunkType == 9) {

                    world[x][y] = 3;

                } else if(tempChunkType == 10) {

                    world[x][y] = 4;

                }

            } else if(world[posX][posY] == 3) {

                tempChunkType = 1 + (Math.floor(Math.random() * 10));
                if(tempChunkType == 1 || tempChunkType == 2 || tempChunkType == 3 || tempChunkType == 4 || tempChunkType == 5) {

                    world[x][y] = 3;

                } else if(tempChunkType == 6 || tempChunkType == 7 || tempChunkType == 8) {

                    world[x][y] = 2;

                } else if(tempChunkType == 9) {

                    world[x][y] = 1;

                } else if(tempChunkType == 10) {

                    world[x][y] = 4;

                }

            } else if(world[posX][posY] == 4) {

                tempChunkType = 1 + (Math.floor(Math.random() * 10));
                if(tempChunkType == 1 || tempChunkType == 2 || tempChunkType == 3 || tempChunkType == 4 || tempChunkType == 5) {

                    world[x][y] = 4;

                } else if(tempChunkType == 6 || tempChunkType == 7 || tempChunkType == 8) {

                    world[x][y] = 3;

                } else if(tempChunkType == 9) {

                    world[x][y] = 5;

                } else if(tempChunkType == 10) {

                    world[x][y] = 2;

                }

            } else if(world[posX][posY] == 5) {

                tempChunkType = 1 + (Math.floor(Math.random() * 10));
                if(tempChunkType == 1 || tempChunkType == 2 || tempChunkType == 3 || tempChunkType == 4 || tempChunkType == 5) {

                    world[x][y] = 5;

                } else if(tempChunkType == 6 || tempChunkType == 7 || tempChunkType == 8) {

                    world[x][y] = 4;

                } else if(tempChunkType == 9) {

                    world[x][y] = 3;

                } else if(tempChunkType == 10) {

                    world[x][y] = 2;

                }

            }

            posX = x;
            posY = y;

        }

    }


}

}

命令类:

package finalproject;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class ControlPanel extends JPanel implements ActionListener {

public String chunkType;
public WorldDisp world = new WorldDisp();
public double[][] worldOb = new double[world.world.length][world.world.width];
public double[][] heightOb = new double[world.world.length][world.world.width];
public int posX = 0;
public int posY = 0;
public int columns = 20;
public int rows = 5;
public JTextField textField = new JTextField(columns);
public JTextArea textArea = new JTextArea(rows, columns);
public JScrollPane scrollPane = new JScrollPane(textArea);
public String newline = "\n";
public String input;

public ControlPanel() {

    setLayout(new BorderLayout());

    scrollPane.setPreferredSize(new Dimension(400, 50));
    textArea.setLineWrap(true);
    textArea.setEditable(false);

    add(textField, BorderLayout.NORTH);
    add(scrollPane, BorderLayout.CENTER);

    textField.addActionListener(this);

    setOb();

}

@Override
public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub

    textArea.setText(null);

    input = textField.getText();

    textArea.append(textField.getText());
    textField.selectAll();

    commandCheck(input);



}

public void displayPosAndChunk(int posX, int posY, String chunkType) {

    textArea.setText(null);

    textArea.append("X: " + posX + " Y: " + posY + newline);
    System.out.println("You are in a " + chunkType);
    textArea.append("You are in a " + chunkType);

}

public void commandCheck(String input) {

    if(input.equalsIgnoreCase("right")) {

        posX ++;
        System.out.println("Go right");

    } else if(input.equalsIgnoreCase("left")) {

        posX --;
        System.out.println("Go left");

    } else if(input.equalsIgnoreCase("down")) {

        posY ++;
        System.out.println("Go down");

    } else if(input.equalsIgnoreCase("up")) {

        posY --;
        System.out.println("Go up");

    } else {

        //Send text to textArea in ControlPanel

    }

    System.out.println("At call for displayPosAndChunk. Before this is all fine. X: " + posX + " Y: " + posY);

    System.out.println(worldOb[posX][posY]);

    displayPosAndChunk(posX, posY, chunkIntToString(worldOb[posX][posY]));

}

public String chunkIntToString(double chunkTypeInt) {

    System.out.println(chunkTypeInt);

    if(chunkTypeInt == 1) {

        chunkType = "desert";

    } else if(chunkTypeInt == 2) {

        chunkType = "plains";

    } else if(chunkTypeInt == 3) {

        chunkType = "lightForest";

    } else if(chunkTypeInt == 4) {

        chunkType = "darkForest";

    } else if(chunkTypeInt == 5) {

        chunkType = "pineForest";

    } else {

        chunkType = "nothing. Something is wrong.";

    }

    System.out.println(chunkType);

    return chunkType;

}

public void setOb() {

    worldOb = world.world.world;

}

}

我的代码有什么问题?我试过改变我调用数组的方式。还有一点需要注意,我知道数组生成正确,因为我有一个类 (WorldDisp),它使用 WorldGen 生成数组并完美显示它。那么,它发生了什么?

【问题讨论】:

  • 张贴Short, Self Contained, Correct Example (SSCCE) 证明您的问题将帮助您获得更好的答案。
  • 告诉我们出了什么问题。如果有未处理的异常,请发布堆栈跟踪。如果它不起作用,请告诉我们预期的行为。

标签: java arrays swing


【解决方案1】:

所有这些位置都从零开始,然后您过多的if 语句检查几乎所有为零的东西。

【讨论】:

    【解决方案2】:

    WorldGen.worldSmooth(int posX, int posY) 未将块类型分配给第一行或第一列。您应该从 0 开始 for 循环(但忽略 0,0)。由于 0 不是有效的块类型,因此您将始终处于沿这些边缘的“有问题”的空间中。

    commandCheck(String input) 没有处理出界的情况(例如,从一开始就向左或向上将移动到负索引)。因此,访问worldOb[posX][posY] 可能会引发未处理的异常。也许你应该检查一个动作是否将你带到一个有效的位置,或者阻止一个无效的动作,或者绕到另一边。

    世界需要double 类型有什么原因吗?您可以将它们表示为 intenum

    还要仔细检查您传递的种子是否有效。一个无效的种子,比如 -1,会产生一个 0.0s 的数组。

    否则,由于您已确认正确生成了数组,因此您的代码中的其他地方可能正在访问不同的实例。

    【讨论】:

      猜你喜欢
      • 2019-02-20
      • 2012-11-03
      • 1970-01-01
      • 2021-12-14
      • 2013-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-25
      相关资源
      最近更新 更多