I know; this is getting bad and I did try to come up with a possible solution. Basically, my program is opening up multiple applets when it should open only one and not displaying any of the random math questions I want it to. This is the code I’ve got:
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);
}
}
}
The multiple frames are caused by this
forloop:It’s closed after the
JFrameis created, so it makes multiple ones. Probably should be closed here:There may still be
ArrayIndexOutOfBoundsExceptionthrown from the loop, though (at least in my very brief experimentation).This means you are requesting access to an array index that doesn’t exist, e.g. your array is 4 elements long, and you want element
[16]. If I may be so bold as to troubleshoot this, I reckon it’s coming from thei <= numberProblems. It should probably bei < numberProblems.Since array indexing starts at 0, the last element will be
theArray.length - 1, nottheArray.length.