below is my current code for a math game i'm having problems creating the answer checking part of it, any assistance is appreciated.

import javax.swing.*;
import java.util.Random;
import java.awt.Button;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Event;
import java.awt.event.*;

import javax.swing.*;


public class MathPanel extends JPanel {
// Declares  Labels and Buttons
	public JButton AddButt , SubButt , MultiButt , DivideButt , Submit ;
	public JTextField Answer ;
	public JLabel Select , Result , Counter ,ImgLbl, Question  ;
	int CountR , CountW; 

	
	
	
	public MathPanel(){
// Sets Up the Gui interface
		ImageIcon icon = new ImageIcon ("blank.png");
		ImageIcon icon1 = new ImageIcon ("sad.png");
		ImageIcon icon2 = new ImageIcon ("happy.png");
		setPreferredSize (new Dimension(300, 400));
	    setBackground (Color.WHITE);
	   
	    
		Select = new JLabel ("Choose Your Question Type:");
		Question= new JLabel ("Question Is Yet To Be Generated");
		Result = new JLabel ("Submit Your Answer When You Are Ready");
		ImgLbl = new JLabel (icon);
		Counter= new JLabel ("Right:" + CountR + " & Wrong :" + CountW);
		AddButt = new JButton ("Add");
		SubButt = new JButton ("Subtract");
		MultiButt = new JButton ("Multiply");
		DivideButt = new JButton ("Divide");
		Submit = new JButton ("Submit");
		//creates listeners to spawn questions when buttons are clicked
		AddButt.addActionListener(new TempListener());
		SubButt.addActionListener(new TempListener());
		MultiButt.addActionListener(new TempListener());
		DivideButt.addActionListener(new TempListener());
		Answer = new JTextField(10);
		Answer.addActionListener(new TempListener());
		Submit.addActionListener(new AnswerListener());
		//puts the buttons and labels on the gui
		
		add (Select);
		add (AddButt);
		add (SubButt);
		add (MultiButt);
		add (DivideButt);
		add (Question);
		add (Answer);
		add (Submit);
		add (Counter);
		add (Result);
		add (ImgLbl);
	}
		private class TempListener implements ActionListener{

			int R1 , R2 ;
				Random generator= new Random();
				
				public void actionPerformed(ActionEvent Event) {
			if (Event.getSource()==(AddButt)){
				R1 = generator.nextInt(10)+1;
				R2 = generator.nextInt(10)+1;
				Question.setText(R1 + "+" + R2) ;
			}
			
			 if (Event.getSource()==(SubButt)){
				R1 = generator.nextInt(10)+1;
				R2 = generator.nextInt(10)+1;
				Question.setText(R1 + "-" + R2) ;
			 }
			if (Event.getSource()==(MultiButt)){
					R1 = generator.nextInt(10)+1;
					R2 = generator.nextInt(10)+1;
					Question.setText(R1 + "*" + R2) ;	
						}
			if (Event.getSource()==(DivideButt)){
				R1 = generator.nextInt(10)+1;
				R2 = generator.nextInt(10)+1;
				Question.setText(R1 + "/" + R2) ;
						
						}

Recommended Answers

All 4 Replies

At the four places where you construct the question you can also calculate the correct answer, and save it in a variable so you can check the user's answer against that value when they press "submit".

At the four places where you construct the question you can also calculate the correct answer, and save it in a variable so you can check the user's answer against that value when they press "submit".

ya my problem is when im trying to get the text from the textfield and compare it to my saved integer answer

You need to parse the text from the textfield to get its integer value, Have a look at the Integer.parseInt method.

if (Event.getSource()==Submit)   {
				
			String text = Answer.getText();
			Temp=Integer.parseInt(text);
				if (Temp.equals(Ans)){
				Result.setText("You are Correct");
				
				ImgLbl = new JLabel (icon2);
				CountR++ ;
				}
				else
					 Result.setText("You are Wrong");
					CountW++ ;
					ImgLbl = new JLabel (icon1);

When I hit my submit button it doesnt check anything nor does my counter update

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.