I have been teaching myself Java for the past week or so. I'm brand new to programming. My code compiles just fine, but the applet does not display any text fields and the genRandomNumbers() method doesn't seem to be working. Could you point me in the right direction? Any help is greatly appreciated!
Here is my code:
//Generate 2 random numbers
//Post a question to multiply the two numbers
//Verify the answer entered
//Post a new question if the solution is correct
import java.awt.event.*; import java.awt.*; import javax.swing.*; public class LearnMultiplication extends JApplet implements ActionListener { JLabel answerLabel; JTextField answerTextField, commentTextField, questionTextField; int random1, random2; public void init() { Container c = getContentPane(); c.setLayout(new FlowLayout() ); JTextField questionTextField = new JTextField(30); c.add(questionTextField); JLabel answerLabel = new JLabel("Enter you answer here"); c.add(answerLabel); JTextField answerTextField = new JTextField(5); answerTextField.addActionListener(this); c.add(answerTextField); JTextField commentTextField = new JTextField(30); c.add(commentTextField); genRandomNumbers(); // invoke method to generate 2 random numbers } public void actionPerformed (ActionEvent e) { int a = Integer.parseInt(e.getActionCommand() ); verifyAnswer(a); // invoke method to verify the product } //method to generate 2 random numbers public void genRandomNumbers() { random1= 1 + (int)(Math.random() * 9 ); random2 = 1 + (int)(Math.random() * 9 ); questionTextField.setText("Multiply " + Integer.toString(random1) + "and " + Integer.toString(random2) + "."); } // method to verify the product of the 2 random numbers public void verifyAnswer(int answer) { int correctAnswer = random1 * random2; if ( correctAnswer == answer) { commentTextField.setText("Very Good!"); genRandomNumbers(); //call the method again to generate 2 new random numbers } else { commentTextField.setText("No, try again!!"); } } }