0

我正在用 Java 为大学课程制作一个 Hangman 游戏,但我的代码有问题。我已经让它运行并打开,但是当我在文本框中输入一个字母进行猜测时,我收到了这个错误:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0
at hangman.HangmanFrame.guessButtonActionPerformed(HangmanFrame.java:164)
at hangman.HangmanFrame.access$000(HangmanFrame.java:14)
at hangman.HangmanFrame$1.actionPerformed(HangmanFrame.java:75)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6290)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6055)
at java.awt.Container.processEvent(Container.java:2039)
at java.awt.Component.dispatchEventImpl(Component.java:4653)
at java.awt.Container.dispatchEventImpl(Container.java:2097)
at java.awt.Component.dispatchEvent(Component.java:4481)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4575)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4236)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4166)
at java.awt.Container.dispatchEventImpl(Container.java:2083)
at java.awt.Window.dispatchEventImpl(Window.java:2482)
at java.awt.Component.dispatchEvent(Component.java:4481)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:648)
at java.awt.EventQueue.access$000(EventQueue.java:84)
at java.awt.EventQueue$1.run(EventQueue.java:607)
at java.awt.EventQueue$1.run(EventQueue.java:605)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
at java.awt.EventQueue$2.run(EventQueue.java:621)
at java.awt.EventQueue$2.run(EventQueue.java:619)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:618)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

根据列表中的第一个错误,我怀疑它与这段代码有关:

    do
    {
        if (playerGuess.equals(arrayToGuess[c]))
        {
            correct = true;
            lettersToGet[c] = playerGuess;
        }

        c++;
    }
    while (c < 6);

    c = 0;

进一步来说:

if (playerGuess.equals(arrayToGuess[c]))

我不知道这有什么问题,因为这是我第一次使用字符串数组并将数组每个部分中的字符与用户输入的字符进行比较。希望有人可以提供帮助。如果您需要更多信息,请告诉我,我会尽快回复您。我还创建了一个短期 Pastebin,因此可以查看整个代码以帮助发现问题:Pastebin Link

谢谢

4

1 回答 1

3

您的问题出在split方法上-它用 reqexp 拆分字符串,而 dot 具有特殊含义,您必须设置

    String breaker = "[.]";

或者

    String breaker = "\\.";

顺便说一句:如果您需要获取字符串的第 i 个字符,您可以使用s.charAt( i )并且不需要拆分 ;-)

于 2012-04-22T13:45:02.720 回答