Java数组索引越界

发布于 2024-09-09 07:08:23 字数 306 浏览 2 评论 0原文

好的,我的代码的一部分似乎出现了数组索引越界错误。具体在第 85-102 行...

我的代码: http://www.sosos.pastebin.com/ f0JQBWui

我只是想让它提前检查是否有被阻挡的图块,这样我的精灵就不会朝它不能移动的方向移动。仅当我位于地图的右角或下角时,才会发生此异常。

我猜为什么会发生这个错误,因为当我在拐角处时..它会检查其右侧和底部不存在的瓷砖...

OK so I seem to be getting an Array Index out of Bounds error in a part of my code. Specifically in lines 85-102...

My code: http://www.sosos.pastebin.com/f0JQBWui

I just want it to check for blocked tiles AHEAD of time that way my sprite doesn't move in the direction it can't. This exception only happens when I am on the RIGHT or BOTTOM corners of my map.

My GUESS of why this error happens if because when I am on the corner.. it checks for the tiles to the RIGHT and BOTTOM of it which are not there...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

み零 2024-09-16 07:08:23

1)你实现blocked(tx,ty)的方式,它只接受合法的棋盘坐标(0<=tx<=12和0<=ty<=8)。否则,它会检查非法数组位置,并产生 ArrayIndexOutOfBoundsException。你确定这是你的意图吗?我认为将板外瓷砖视为阻塞是有道理的。

2)第85-102行似乎有很多错误。我认为你的意思是这样的:

        if (spawnX == 0 || blocked(spawnX - 1, spawnY)) {
            left = false;
            System.out.println("You can't go left!");
        }           
        if (spawnX == 12 || blocked(spawnX + 1, spawnY)) {
            right = false;
            System.out.println("You can't go right!");
        }
        if (spawnY ==0 || blocked(spawnX, spawnY - 1)) {
            up = false;
            System.out.println("You can't go up!");
        }
        if (spawnY == 8 || blocked(spawnX, spawnY + 1)) {
            down = false;
            System.out.println("You can't go down!");
        }

无论如何,如果你按照我的建议修复(1),每个方向的额外约束条件是不必要的。

3) isInBound(r,c) 实现不正确。由于 c 上的条件,它总是返回 false。

4)代码还有很多其他问题,我就不细说了。原则上,尽量使设计简单并确保代码不会重复。

1) The way you implemented blocked(tx,ty), it only accepts legal board coordinates (0<=tx<=12 and 0<=ty<=8). Otherwise it checks an illegal array position, producing an ArrayIndexOutOfBoundsException. Are you sure this is your intention? I think it makes sense to consider off board tiles as blocked.

2) In lines 85-102 there seems to be many errors. I think you meant something like:

        if (spawnX == 0 || blocked(spawnX - 1, spawnY)) {
            left = false;
            System.out.println("You can't go left!");
        }           
        if (spawnX == 12 || blocked(spawnX + 1, spawnY)) {
            right = false;
            System.out.println("You can't go right!");
        }
        if (spawnY ==0 || blocked(spawnX, spawnY - 1)) {
            up = false;
            System.out.println("You can't go up!");
        }
        if (spawnY == 8 || blocked(spawnX, spawnY + 1)) {
            down = false;
            System.out.println("You can't go down!");
        }

Anyway, if you fix (1) as I suggested, the extra bound condition per direction is unecessary.

3) isInBound(r,c) is implemented incorrectly. It always returns false, due to the conditions on c.

4) There are many other problems with the code, but I will not enter into details. As a principle, try to make your design simple and make sure the code does not repeat itself.

氛圍 2024-09-16 07:08:23

您必须在 blocked() 函数中进行一些边界检查。确保他们给你的坐标确实存在,如果不存在,则返回一些“阻止”值。

You're going to have to do some bounds-checking in your blocked() function. Make sure that the coordinates they're giving you actually exist and return some "blocked" value if they don't.

流绪微梦 2024-09-16 07:08:23

在底部或右侧获取错误的描述似乎表明您需要测试该值是否超出数组边界。看看 Array.length

The description of getting the error at the bottom or right would seem to suggest that you need to test if the value exceeds the array bounds. Have a look at Array.length

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文