我正在用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]))


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

谢谢

最佳答案

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

    String breaker = "[.]";


要么

    String breaker = "\\.";


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

10-01 00:28
查看更多