I know; It's getting worse and I tried to come up with the possible solution. In fact, many programs are opening up in my program when it should open only one and should not show any question of random mathematics that I want . I have this code:
package randommath game; Import java.awt.GridLayout; Import java.awt.event.ActionEvent; Import java.awt.event.ActionListener; Import javax.swing *; Public class randommath {public static zero main (string [] args) {RandomProblem Generator randomProblems = new RandomProblem Generator (10); Final int number problems = 10; Int correctScore = 0; Zpean panel = new zpn (); Jepiel [] questionspanel = new jepiel [numbers]; Jepinal main question paper = new jpn (); Zeppel main panele = new zpn (); Integer answer; Int correct answer; JLABEL [] MathPrablemes = New JLABEL [Number Problems]; Ultimate Jettextfield [] Mathematical Area = New JTextField [Numbers]; Jlabble [] right no = new jlabell [numbers]; Final JLab score = new JLABEL (rightcore + "/ 10"); Pocket-submit = new pocket ("submit"); MainQuestionPanel.setLayout (new grid layout (1, 10)); For (int i = 1; i & lt; = number problems; i ++) {last int x = randomProblems.createNumberX (); Last Int Y = Random Pub. Mathematical Problems [i] = New JLabel ("+ +" * "+ Y +" = "); Math Orientation [i] = New JTextField (); String AnswerSat = Math Angers [i] .gettext (); if (AnswerStr.isEmpty ()) {Not right [i] = New JLabel ("No valid answer / answer field empty!");} And {answer = integer.partyInt (mathematical basis [i] .gettext ( Correct reply = x * y; if (answer == correct answer) {right yes not [i] = new jlabel ("correct answer; good work!") CorrectScore = correctScore + 1;} and {right} No [i] = new jlabel ("Wrong answer, try again!");} Questionpanel [i] .add (Math Problem [i]); questionPanel [i] .add (mathAnswers [i]); questionPanel [i] .add (correctYesNo [i ]; MainQuestionPanel.add (questionPanel [i]);} Ultimate integer temp = correctScore; Submit.addActionListener (New Executioner) {@ Override Public Wide Action Performed (Action Item E) {Score.Set Text ("Score: + Temporary + "/ 10");}}); panel.add (submitted); panel.add (score); MainPanel.setLayout (new grid layout (1, 2)); mainPanel.add (mainQuestionPanel); MainPanel.add (panel); JFram GameFrame = New JFram (); GameFrame.setTitle ("Random Math Game"); GameFrame.setSize (1000, 1000); GameFrame.setVisible (true); GameFrame.setContentPane (mainPanel); }}}}
Multiple frames are due to this Loop: for (int i = 1; i & lt; = numberproblems; i ++) {... this JFrame is built after is built, so it makes many people. Perhaps it should be closed here: final integer temp = correctScore; Submit.addActionListener (New Executioner) {@ Override Public Wide Action Performed (Action Score: "Score:" + Temporary + "/ 10");}}); } // & lt; - Close panel here for loop. Add (Submit); still could be thrown with ArrayIndexOutOfBoundsException loop, though (at least in my very low use). This means that you request access to an array index that does not exist, eg your array is 4 elements long, and you want element [16] . If I can be so bold to troubleshoot this problem, then I think that this i & lt; = Problems coming from . This is probably i . Since the array starts with index 0, the last element theArray.length - 1 will be, theArray.length will not be.
Comments
Post a Comment