首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >扫雷游戏异常错误

扫雷游戏异常错误
EN

Stack Overflow用户
提问于 2012-02-11 11:04:27
回答 3查看 388关注 0票数 0

您好,我目前正在尝试创建一个扫雷舰游戏,我有我的图形用户界面,并使用一个二维数组来存储一个位置是否有一个地雷,然而,当我尝试让游戏结束点击一个地雷使用以下代码:

代码语言:javascript
复制
if (board[row][col] == 1) {
    return GameStatus.Lost; }  
else {
    return GameStatus.Continue;
 }

我收到错误信息,因为

代码语言:javascript
复制
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 10
at Game.getGameStatus(Game.java:55)
at MineSweeperPanel$ButtonListener.actionPerformed(MineSweeperPanel.java:71)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
EN

回答 3

Stack Overflow用户

发布于 2012-02-11 11:07:50

检查您在rowcol中调用的边界。例如,如果您有25行和25列,并且您引用的是board[25][25],那么这就超出了数组的界限。虽然行数的总大小是25,但在数组中,索引将从025-1

票数 0
EN

Stack Overflow用户

发布于 2012-02-11 11:08:10

数组索引越界意味着你的数组中有(比方说) 10个元素,但你试图访问(比方说)第11个元素-它根本不存在。

健全性检查-数组从0开始索引,您在rowcol中的值是否从1开始索引?

票数 0
EN

Stack Overflow用户

发布于 2012-02-11 11:42:54

当它发生时?

当您尝试访问索引超过其长度的数组时,会发生越界异常。java数组的最大索引为(length -1),例如:

代码语言:javascript
复制
String [] stringArray = new String[10];
stringArray[10]
// the code above will produce an out of bounds exception, because the it bigger than length -1, which is 10 - 1 = 9.

如果您不知道数组的大小或长度,可以从stringArray.length中知道。

怎么处理呢?

你应该确保你的程序不会访问索引长度大于- 1的数组。例如:

代码语言:javascript
复制
for(int i=0;i<stringArray.lenght;i++) {
    //write your code here
}

上面的代码将保证stringArray永远不会被访问超过它的最大索引。

你的案例

在您的情况下,您必须定义了数组限制,并尝试访问超出定义限制范围的数组数据。

另请阅读this以了解更多信息...

Example of 2d array having out of bound exception

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9237482

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档