welll it is just a program that generates 2 random numbers for the user...the user inputs their answer and if it is correct it adds 1pt to their score. But then it is also supposed to generate a new problem, but I do not know how to code that in.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
 
/* Figure out how to create a new set of problms
 * Type a list of specifications, include a list of test cases
 * DONE :]
 * **/
 
package javaapplication1;
 
import java.awt.GridLayout;
import java.awt.Window;
import javax.swing.*;
import java.awt.event.*;
 
 
/**
 *
 * @author Baba 
 */
 
 class Grid extends JFrame{
    int done = 0;
    final int score = 0;
    final int total = 0;            
        //Create Panels
 
        JPanel p2 = new JPanel();
        JPanel p3 = new JPanel();
        final JPanel p1 = new JPanel();
 
        //Create Radio buttons & group them
        ButtonGroup group = new ButtonGroup();
        final JRadioButton ADD = new JRadioButton("Addition");
        final JRadioButton SUB = new JRadioButton("Subtraction");
        final JRadioButton MUL = new JRadioButton("Multiplication");
        final JRadioButton DIV = new JRadioButton("Division");
 
 
        //Create buttons
        JButton NEXT = new JButton("NEXT");
        JButton END = new JButton("End");
 
        //Create Labels
        JLabel l1 = new JLabel("First num");
        JLabel l2 = new JLabel("Second num");
        JLabel l3 = new JLabel("Answer:");
        JLabel l4 = new JLabel("Score:");
        final JLabel l5 = new JLabel("");
        JLabel l6 = new JLabel("/");
        final JLabel l7 = new JLabel("");
 
 
        //Create Textfields
        final JTextField number = new JTextField(Generator1());
        final JTextField number2 = new JTextField(Generator1());
        final JTextField answer = new JTextField(5);
 
 
    
    Grid(){
        setLayout(new GridLayout(4, 4, 2 , 2));
 
        p2.add(ADD);
        p2.add(SUB);
        group.add(ADD);
        group.add(SUB);
        group.add(MUL);
        group.add(DIV);
 
        p2.add(ADD);
        p2.add(SUB);
        p2.add(DIV);
        p2.add(MUL);
 
 
        //Add to panels
 
        p1.add(l1);
        p1.add(number);
        p1.add(l2);
        p1.add(number2);
        p1.add(l3);
        p1.add(answer);
        p1.add(l4);
        p1.add(l5);
        p1.add(l6);
        p1.add(l7);
        p3.add(NEXT);
        p3.add(END);
 
        //Add panels
 
        add(p2);
        add(p1);
        add(p3);
 
 
 
      
        //Create Listners
        Listeners();
 
    }
 
void Listeners(){
      NEXT.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
 
         int answer1 = 0;
         //Grab the numbers entered
         int numm1 = Integer.parseInt(number.getText());
         int numm2 = Integer.parseInt(number2.getText());
         int nummsanswer = Integer.parseInt(answer.getText());
 
         //Set the score and total into new variabls
         int nummscore = score;
         int nummtotal = total;
 
         //Check if the add radio button is selected if so add
         if (ADD.isSelected() == true){
             answer1 = numm1 + numm2;
         }
 
         //otherwise check if the subtract button is selected if so subtract
         else if (SUB.isSelected() == true){
             answer1 = numm1 - numm2;
         }
 
         //check if the multiplication button is selected if so multiply
         else if (MUL.isSelected() == true){
             answer1 = numm1 * numm2;
         }
 
         //check if the division button is selected if so divide
         else if (DIV.isSelected() == true){
             answer1 = numm1 / numm2;
         }
 
 
         //If the answer user entered is the same with th true answer
         if (nummsanswer == answer1){
 
             //add to the total and score
             nummtotal += 1;
             nummscore += 1;
             //Convert the input back to String
             String newscore = String.valueOf(nummscore);
             String newtotal = String.valueOf(nummtotal);
 
             //Set the text
             l5.setText(newscore);
             l7.setText(newtotal);
 
         }
 
         //Otherwise just increase the total counter
         else {
             nummtotal += 1;
             String newtotal = String.valueOf(nummtotal);
             l7.setText(newtotal);
         }
 
 
 
 
      }
 
 
 
    });
 
  //Create End listener
END.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        // get the root window and call dispose on it
 
        Window win = SwingUtilities.getWindowAncestor(p1);
        win.dispose();
 
      }
    });
 
//new Grid();
 
}
 
 String Generator1(){
     int randomnum;
     randomnum = (1 + (int)(Math.random() * 100));
     String randomnumm = String.valueOf(randomnum);
     return randomnumm;
 
   }
 
 
}
 
public class Main {
 
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
       
        JFrame frame = new Grid();
        frame.setTitle("Flashcard Testing");
        frame.setSize(500, 200);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
 
 
 
 
    }
 
}

Recommended Answers

All 4 Replies

Do you want it to be a "random" problem or do you want it to be a problem that you made up in advance, chosen from a list? I'm not going to waste time explaining how to do both when you only need one or the other, so tell us which it is.

Yes That is exactly what I want it to do BestJew! please help and I apologize I didn't get more specific.

Not only did you PM me about this issue, which is technically against the rules, but you didn't answer my question. Do you want the problems to be chosen from a list of problems which were previously created in advance, or do you want to "randomly" generate a problem?

Oops sorry, and I want it to randomly generate a problem

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.