Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8211359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:24:08+00:00 2026-06-07T10:24:08+00:00

I know; this is getting bad and I did try to come up with

  • 0

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);



        }
  }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T10:24:09+00:00Added an answer on June 7, 2026 at 10:24 am

    The multiple frames are caused by this for loop:

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

    It’s closed after the JFrame is created, so it makes multiple ones. Probably should be closed here:

        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);
    

    There may still be ArrayIndexOutOfBoundsException thrown 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 the i <= numberProblems. It should probably be i < numberProblems.

    Since array indexing starts at 0, the last element will be theArray.length - 1, not theArray.length.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am getting to know boost::variant. I think this example should work. #include <boost/fusion/sequence.hpp>
I know this doubt could be silly mistake. I am getting a variable from
I know this might seem like a duplicate, but I'm not getting anywhere with
I keep getting this error and I really dont know why. I have tried
Does anyone know why I am getting this error: The model item passed into
While deploying a dwr web app, i'm continuously getting this error. I dont know
I'm getting a Object doesn't support this property or method error, does anyone know
I know more or less how to do this, but I think I'm getting
I'm having trouble getting any information to display from this query. Anyone know where
First of all I know this is probably bad sql but I just need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.