Java 读取数组中所有位置的值为零
我正在用Java和Swing制作一个游戏。我使用2D数组来存储我的世界。我有一个文本命令系统,当它从数组中调用变量时,除了0,0之外,它在每个位置都返回0。这是我的密码: 用于生成数组的类:Java 读取数组中所有位置的值为零,java,arrays,swing,Java,Arrays,Swing,我正在用Java和Swing制作一个游戏。我使用2D数组来存储我的世界。我有一个文本命令系统,当它从数组中调用变量时,除了0,0之外,它在每个位置都返回0。这是我的密码: 用于生成数组的类: package finalproject; public class WorldGen { public int length = 20; public int width = 20; public double[][] world = new double[length][width]; publi
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;
}
}
}
}
我的代码怎么了?我已尝试更改调用数组的方式。还有一点需要注意的是,我知道数组正在正确生成,因为我有一个类(WorldDisp),它使用WorldGen生成数组并完美地显示它。那么,它发生了什么呢?所有这些位置都从零开始,然后你过多的
if
语句检查除零之外的所有内容 WorldGen.worldSmooth(int-posX,int-posY)
未将块类型分配给第一行或第一列。应在0处启动for
循环(但忽略0,0)。由于0不是有效的块类型,因此沿着这些边,您将始终处于“有问题”空间中
commandCheck(字符串输入)
不处理走出世界的情况(例如从一开始向左或向上移动将进入负索引)。因此,访问worldOb[posX][posY]
可能会引发未被处理的异常。也许你应该检查一次移动是否会将你带到一个有效的位置,或者阻止一次无效的移动,或者绕到另一边
是否有理由认为世界需要是双重的?您可能会将它们表示为int
或enum
还要再次检查您传递的种子是否有效。无效种子(如-1)将导致0.0的数组
否则,由于您已确认正确生成了数组,您可能正在代码的其他地方访问不同的实例。发布一个演示您的问题的示例将有助于您获得更好的答案。您可以告诉我们出了什么问题。如果存在未处理的异常,则发布堆栈跟踪。如果不起作用,请告诉我们预期的行为。
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;
}
}