1

我知道; 这越来越糟糕,我确实试图想出一个可能的解决方案。基本上,当我的程序应该只打开一个而不显示任何我想要的随机数学问题时,它会打开多个小程序。这是我得到的代码:

package RandomMathGame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class RandomMathGame {

    public static void main(String[] args) {
        RandomProblemGenerator randomProblems = new RandomProblemGenerator(10);
        final int numberProblems = 10;
        int correctScore = 0;
        JPanel panel = new JPanel();
        JPanel[] questionPanel = new JPanel[numberProblems];
        JPanel mainQuestionPanel = new JPanel();
        JPanel mainPanel = new JPanel();
       int answer;
        int correctAnswer;
        JLabel[] mathProblems = new JLabel[numberProblems];
        final JTextField[] mathAnswers = new JTextField[numberProblems];
        JLabel[] correctYesNo = new JLabel[numberProblems];
        final JLabel score = new JLabel(correctScore + "/10");
        JButton submit = new JButton("Submit");

        mainQuestionPanel.setLayout(new GridLayout(1, 10));

        for (int i = 1; i <= numberProblems; i++)
        {
            final int X = randomProblems.createNumberX();
            final int Y = randomProblems.createNumberY();

            mathProblems[i] = new JLabel("" + X + " * " + Y + " = ");
            mathAnswers[i] = new JTextField();

         String answerStr = mathAnswers[i].getText();

    if(answerStr.isEmpty()){

          correctYesNo[i] = new JLabel("Not a valid answer/answer field empty!");
    } else {

             answer = Integer.parseInt(mathAnswers[i].getText());
             correctAnswer = X * Y;

            if (answer == correctAnswer)
            {
                correctYesNo[i] = new JLabel("Correct answer; good job!");
                correctScore = correctScore + 1;
            }
            else
            {
               correctYesNo[i] = new JLabel("Incorrect answer; try again!");

            }
             questionPanel[i].add(mathProblems[i]);
             questionPanel[i].add(mathAnswers[i]);
             questionPanel[i].add(correctYesNo[i]);
             mainQuestionPanel.add(questionPanel[i]);
            }

        final int temp = correctScore;
      submit.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                score.setText("Score: " + temp + "/10");
            }
        });



        panel.add(submit);
        panel.add(score);
        mainPanel.setLayout(new GridLayout(1, 2));
        mainPanel.add(mainQuestionPanel);
        mainPanel.add(panel);
        JFrame gameFrame = new JFrame();
        gameFrame.setTitle("Random Math Game");
        gameFrame.setSize(1000, 1000);
        gameFrame.setVisible(true);
        gameFrame.setContentPane(mainPanel);



        }
  }
}
4

2 回答 2

2

问题是gameFrame在 for 循环中创建(并显示)。gameFrame在创建对象之前尝试关闭循环。

于 2012-07-10T04:47:22.633 回答
1

多个帧是由这个for循环引起的:

for (int i = 1; i <= numberProblems; i++)
{
...

它在创建后关闭JFrame,因此它会生成多个。可能应该在这里关闭:

    final int temp = correctScore;
    submit.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            score.setText("Score: " + temp + "/10");
        }
    });
}  // <- CLOSE THE FOR LOOP HERE

panel.add(submit);

不过,可能仍然会ArrayIndexOutOfBoundsException从循环中抛出(至少在我非常简短的实验中)。

这意味着您正在请求访问一个不存在的数组索引,例如您的数组有 4 个元素长,并且您想要 element [16]。如果我敢于解决这个问题,我认为它来自i <= numberProblems. 应该是i < numberProblems

由于数组索引从 0 开始,最后一个元素将是theArray.length - 1,而不是theArray.length

于 2012-07-10T04:46:30.330 回答