Arnulfo890
Arnulfo890

Reputation: 3

How to display and calculate random numbers in a JLabel?

I am trying to create a program where I could calculate 2 randomly generated numbers and display them in a JLabel. The program is successfully displaying random numbers in a JLabel but it only calculates the first set of numbers that have been generated.

I want it to be able to calculate all the sets of numbers being generated randomly.

Random dice = new Random();
boolean solution;
JLabel num1,num2,checker;
int calculation;

public void repeatLoop() {
    switch(calculation) {
    case 1:
        int addition1 = Integer.valueOf(num1.getText()) + Integer.valueOf(num2.getText());
        break;
    case 2:
        num1.setText("");
        num2.setText("");
        break;
    case 3:
        num1.setText(String.valueOf(1 + dice.nextInt(6)));
        num2.setText(String.valueOf(1 + dice.nextInt(6)));
        break;
    }
}

public JLabel checkerLabel() {
    JLabel checker = new JLabel("");
    checker.setFont(new Font("Tahoma", Font.BOLD, 13));
    checker.setForeground(Color.GREEN);
    checker.setHorizontalAlignment(SwingConstants.CENTER);
    checker.setBounds(163, 21, 109, 23);
    contentPane.add(checker);
    return checker;
}

public addFrame() {
    JLabel num1 = new JLabel("3");
    num1.setHorizontalAlignment(SwingConstants.CENTER);
    num1.setFont(new Font("Tahoma", Font.PLAIN, 20));
    num1.setBounds(122, 101, 63, 32);
    contentPane.add(num1);

    JLabel num2 = new JLabel("4");
    num2.setHorizontalAlignment(SwingConstants.CENTER);
    num2.setFont(new Font("Tahoma", Font.PLAIN, 20));
    num2.setBounds(268, 101, 63, 32);
    contentPane.add(num2);

    this.num1 = num1;
    this.num2 = num2;
    calculation = 3;
    int addition1 = Integer.valueOf(num1.getText()) + Integer.valueOf(num2.getText());
    JLabel addOperation = new JLabel("+");
    addOperation.setHorizontalAlignment(SwingConstants.CENTER);
    addOperation.setFont(new Font("Tahoma", Font.PLAIN, 20));
    addOperation.setBounds(195, 101, 63, 32);
    contentPane.add(addOperation);

    this.checker = checkerLabel();

    textField1 = new JTextField();
    textField1.setHorizontalAlignment(SwingConstants.CENTER);
    textField1.setBounds(163, 155, 120, 32);
    contentPane.add(textField1);
    textField1.setColumns(10);

    JButton checkBtn1 = new JButton("Check");
    checkBtn1.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            final int addition2 = addition1;
            int ansUser = Integer.parseInt(textField1.getText());
            if(addition2 == ansUser) {
                solution = true;
            } else {
                solution = false;
            }
            if(solution == true) {
                checker.setText("CORRECT");
                checker.setForeground(Color.GREEN);
            } else {
                checker.setText("WRONG");
                checker.setForeground(Color.RED);
            }
            textField1.setText("");
            calculation = 1;
            calculation = 2;
            repeatLoop();
        }
    });
}

Upvotes: 0

Views: 93

Answers (2)

Abra
Abra

Reputation: 20914

You have a few logic bugs in your code. I believe I have fixed them in the below code.

I suggest that you run the below code to make sure that it behaves the way that you want. If it does then compare it with your code to see the differences. There aren't many. Here is a summary:

  1. I made addition1 a class member.
  2. I added a single break statement in the switch statement in method repeatLoop.
  3. I changed the last few lines of method actionPerformed.

Finally, I suggest that you run your original code with a debugger to see why your code has logic problems.

Here is the code.

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.Color;
import java.util.Random;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class addFrame extends JFrame {

    private JPanel contentPane;
    private JTextField textField1;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    addFrame frame = new addFrame();
                    frame.setVisible(true);
                }
                catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    Random dice = new Random();
    boolean solution;
    JLabel num1, num2, checker;
    int calculation;
    int addition1;

    public void repeatLoop() {
        switch (calculation) {
            case 1:
                addition1 = Integer.valueOf(num1.getText()) + Integer.valueOf(num2.getText());
                break;
            case 2:
                num1.setText("");
                num2.setText("");
            case 3:
                num1.setText(String.valueOf(1 + dice.nextInt(6)));
                num2.setText(String.valueOf(1 + dice.nextInt(6)));
        }
    }

    public JLabel checkerLabel() {
        JLabel checker = new JLabel("");
        checker.setFont(new Font("Tahoma", Font.BOLD, 13));
        checker.setForeground(Color.GREEN);
        checker.setHorizontalAlignment(SwingConstants.CENTER);
        checker.setBounds(163, 21, 109, 23);
        contentPane.add(checker);
        return checker;
    }

    public addFrame() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel title3 = new JLabel("ADDITION");
        title3.setForeground(Color.RED);
        title3.setFont(new Font("Verdana", Font.PLAIN, 30));
        title3.setHorizontalAlignment(SwingConstants.CENTER);
        title3.setBounds(108, 47, 224, 32);
        contentPane.add(title3);

        JLabel num1 = new JLabel("3");
        num1.setHorizontalAlignment(SwingConstants.CENTER);
        num1.setFont(new Font("Tahoma", Font.PLAIN, 20));
        num1.setBounds(122, 101, 63, 32);
        contentPane.add(num1);

        JLabel num2 = new JLabel("4");
        num2.setHorizontalAlignment(SwingConstants.CENTER);
        num2.setFont(new Font("Tahoma", Font.PLAIN, 20));
        num2.setBounds(268, 101, 63, 32);
        contentPane.add(num2);

        this.num1 = num1;
        this.num2 = num2;
        calculation = 3;
        addition1 = Integer.valueOf(num1.getText()) + Integer.valueOf(num2.getText());
        JLabel addOperation = new JLabel("+");
        addOperation.setHorizontalAlignment(SwingConstants.CENTER);
        addOperation.setFont(new Font("Tahoma", Font.PLAIN, 20));
        addOperation.setBounds(195, 101, 63, 32);
        contentPane.add(addOperation);

        this.checker = checkerLabel();

        textField1 = new JTextField();
        textField1.setHorizontalAlignment(SwingConstants.CENTER);
        textField1.setBounds(163, 155, 120, 32);
        contentPane.add(textField1);
        textField1.setColumns(10);

        JButton checkBtn1 = new JButton("Check");
        checkBtn1.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                final int addition2 = addition1;
                int ansUser = Integer.parseInt(textField1.getText());
                if (addition2 == ansUser) {
                    solution = true;
                }
                else {
                    solution = false;
                }
                if (solution == true) {
                    checker.setText("CORRECT");
                    checker.setForeground(Color.GREEN);
                }
                else {
                    checker.setText("WRONG");
                    checker.setForeground(Color.RED);
                }
                textField1.setText("");
                calculation = 2;
                repeatLoop();
                calculation = 1;
                repeatLoop();
            }
        });
        checkBtn1.setBounds(183, 211, 89, 23);
        contentPane.add(checkBtn1);
        setLocationRelativeTo(null);
    }
}

Upvotes: 1

Eddie Deng
Eddie Deng

Reputation: 1479

Maybe you should add break after each case in your switch(calculation) structure.

Try to modify the switch statement as:

switch(calculation) {
   case 1:
        int addition1 = Integer.valueOf(num1.getText()) + Integer.valueOf(num2.getText());
        break;
   case 2:
        num1.setText("");
        num2.setText("");
        break;
   case 3:
        num1.setText(String.valueOf(1 + dice.nextInt(6)));
        num2.setText(String.valueOf(1 + dice.nextInt(6)));
        break;
}

Upvotes: 0

Related Questions