为什么我得到一个数组索引越界异常?
问题描述:
Accordin到的调试屏幕的误差是在:为什么我得到一个数组索引越界异常?
1.Line 16:(类RandomLevel)
protected void generateLevel() {
for (int y = 0; y < height; y++) {
for (int x = 0; y < width; x++) {
tiles[x + y * width] = random.nextInt(4); //Here is the error.
}
}
}
2.Line 15:(职业等级)
public Level(int width, int height) {
this.width = width;
this.height = height;
tiles = new int[width * height];
generateLevel(); //Here is the error.
}
3。第10行:(类RandomLevel)
public RandomLevel(int width, int height) {
super(width, height); // Here is the error.
}
4. 43行:(类游戏)
public Game() {
Dimension size = new Dimension(width * scale, height * scale);
setPreferredSize(size);
screen = new Screen(width, height);
frame = new JFrame();
key = new Keyboard();
level = new RandomLevel(64, 64); // Here is the error.
addKeyListener(key);
}
5.Line 124:(类游戏)
public static void main(String[] args) {
Game game = new Game(); // Here is the error.
game.frame.setResizable(false);
game.frame.setTitle(game.title);
game.frame.add(game);
game.frame.pack();
game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
game.frame.setLocationRelativeTo(null);
game.frame.setVisible(true);
game.start();
}
那我该怎么办?我明白什么是例外,但我不知道它为什么出现。帮帮我?
答
您的内部for
循环的条件是错误的。
for (int x = 0; y < width; x++) {
您遍历x
,但你的病情再次涉及y
。尝试
for (int x = 0; x < width; x++) {
+0
感谢你,现在我知道im阻滞。谢谢! – SvelterEagle 2015-03-30 18:30:26
答
你有
for (int x = 0; y < width; x++) {
你打算
for (int x = 0; x < width; x++) {
答
你有两个错误:
1)
for (int x = 0; y < width; x++) {
变化Y到X
2)
tiles = new int[width * height];
但
tiles[x + y * width] = random.nextInt(4); //Here is the error.
这将上升到
tiles[width+height*width]
这将导致一个错误,改变
tiles = new int[width * height];
到
tiles = new int[width + width * height];
仔细查看此条件为'(INT X = 0; y 2015-03-30 18:29:13