user3517155
user3517155

Reputation:

if else statement wont work java

The question field should contain 36 questions, the answer to these question are on the 36 buttons in the grid.

When the user selects the clicks the correct button answer the button is taken off the grid, the aim is to clear the grid so the grid is empty.

if the user clicks an incorrect button the game restarts.

i am having a problem with adding the question label.

the question field should display 36 question starting with what is 0 + 1 when the user clicks the correct button it then shows question 2 shows in the field which is what is 1+1 and so till question 36

i have tried to get this by Changing this line of code

 gui.statusLabel.setText("what is 0+ 1" + gui.buttonCounter);

to this:

 gui.statusLabel.setText("what is " + gui.buttonCounter + "+ 1");

but the games doesnt work properly although the correct answer is selected the code keeps saying doing the "else" in this code ie. Incorrect button clicked, start again: what is 0+ 1

    if(clickedNumber == gui.buttonCounter){
        gui.buttonCounter++;
        buttonClicked.setText("");//optional - clears correct selection
        if(gui.buttonCounter > gui.ROWS*gui.COLUMNS) gui.reset();
        gui.statusLabel.setText("what is 0+ 1" + gui.buttonCounter);
    } else {
        gui.reset();
        gui.statusLabel.setText("Incorrect button clicked, start again: what is 0+ 1");
    }

    }
}

how do i fix this? so when i click the correct answer button it moves to the next question and does not display Incorrect button clicked, start again: what is 0+ 1.

full code

class NewClass {

    final int ROWS = 6;
    final int COLUMNS = 6;
    JButton[] buttons = new JButton[ROWS * COLUMNS];
    JLabel statusLabel = new JLabel("", JLabel.CENTER);
    java.util.List<Integer> buttonNumbers = new ArrayList<Integer>();

    int buttonCounter = 1;

    public NewClass() {
        JPanel buttonPanel = new JPanel(new GridLayout(ROWS, COLUMNS));
        ButtonListener listener = new ButtonListener(NewClass.this);
        for (int x = 0, y = ROWS * COLUMNS; x < y; x++) {
            buttons[x] = new JButton();
            buttons[x].addActionListener(listener);
            buttonPanel.add(buttons[x]);
            buttonNumbers.add(new Integer(x + 1));
        }
        reset();
        JFrame frame = new JFrame();
        frame.getContentPane().add(statusLabel, BorderLayout.NORTH);
        frame.getContentPane().add(buttonPanel, BorderLayout.CENTER);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public void reset() {
        Collections.shuffle(buttonNumbers);
        for (int x = 0, y = ROWS * COLUMNS; x < y; x++) {
            buttons[x].setText(String.valueOf(buttonNumbers.get(x)));
        }
        buttonCounter = 1;
        statusLabel.setText("Please click button  " + buttonCounter);
    }

    public static void main(String[] args) {
        new NewClass();
    }

    class ButtonListener implements ActionListener {
        NewClass gui;

        ButtonListener(NewClass g) {
            gui = g;
        }

        public void actionPerformed(ActionEvent e) {
            JButton buttonClicked = (JButton) e.getSource();
            int clickedNumber = Integer.parseInt(buttonClicked.getText());
            if (clickedNumber == gui.buttonCounter) {
                gui.buttonCounter++;
                buttonClicked.setText("");// optional - clears correct selection
                if (gui.buttonCounter > gui.ROWS * gui.COLUMNS)
                    gui.reset();
                // gui.statusLabel.setText("Please click button" +
                // gui.buttonCounter);

                gui.statusLabel.setText("what is " + buttonCounter);

            } else {
                gui.reset();
                gui.statusLabel
                        .setText("Incorrect button clicked, start again: 0 + 1");
            }

        }
    }
}

Upvotes: 0

Views: 710

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347204

So, taking the code from your previous question and adding

System.out.println("ClickedNumber = " + clickedNumber);
System.out.println("gui.buttonCounter = " + gui.buttonCounter);

Into the actionPerformed method it asks...

"What is 0+1 1" (which should be "What is 0+1"). I click "1", which outputs...

ClickedNumber = 1
gui.buttonCounter = 1

So looking at the if statement;

if (clickedNumber == gui.buttonCounter) {

This is true...

Then it asks...

"What is 2+1". I click "3", which outputs...

ClickedNumber = 3
gui.buttonCounter = 2

So looking at the if statement;

if (clickedNumber == gui.buttonCounter) {

Which is false.

There is a logic error in your code. Basically, it's coming down to you not actually knowing what the question is...

Runnable example

Basically what this does is asks...

"What is " + buttonCounter + "+1"?

In the if condition, we assume the buttonCounter is 0 indexed, meaning we need to increase the comparison by 1

if (clickedNumber == (gui.buttonCounter + 1)) {

Or you could reduce the clickedNumeber by 1

if ((clickedNumber - 1) == gui.buttonCounter) {

Remember, you question is always asks "What's the next number", not what's the current number...

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

class NewClass {

    final int ROWS = 6;
    final int COLUMNS = 6;
    JButton[] buttons = new JButton[ROWS * COLUMNS];
    JLabel statusLabel = new JLabel("", JLabel.CENTER);
    java.util.List<Integer> buttonNumbers = new ArrayList<Integer>();

    int buttonCounter = 1;

    public NewClass() {
        JPanel buttonPanel = new JPanel(new GridLayout(ROWS, COLUMNS));
        ButtonListener listener = new ButtonListener(NewClass.this);
        for (int x = 0, y = ROWS * COLUMNS; x < y; x++) {
            buttons[x] = new JButton();
            buttons[x].addActionListener(listener);
            buttonPanel.add(buttons[x]);
            buttonNumbers.add(new Integer(x + 1));
        }
        reset();
        JFrame frame = new JFrame();
        frame.getContentPane().add(statusLabel, BorderLayout.NORTH);
        frame.getContentPane().add(buttonPanel, BorderLayout.CENTER);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public void reset() {
        Collections.shuffle(buttonNumbers);
        for (int x = 0, y = ROWS * COLUMNS; x < y; x++) {
            buttons[x].setText(String.valueOf(buttonNumbers.get(x)));
        }
        buttonCounter = 0;
        statusLabel.setText("Please click button  " + buttonCounter + "+1");
    }

    public static void main(String[] args) {
        new NewClass();
    }

    class ButtonListener implements ActionListener {

        NewClass gui;

        ButtonListener(NewClass g) {
            gui = g;
        }

        public void actionPerformed(ActionEvent e) {
            JButton buttonClicked = (JButton) e.getSource();
            int clickedNumber = Integer.parseInt(buttonClicked.getText());

            System.out.println("ClickedNumber = " + clickedNumber);
            System.out.println("gui.buttonCounter = " + gui.buttonCounter);
            if (clickedNumber == (gui.buttonCounter + 1)) {
                gui.buttonCounter++;
                buttonClicked.setText("");// optional - clears correct selection
                if (gui.buttonCounter > gui.ROWS * gui.COLUMNS) {
                    gui.reset();
                }
                // gui.statusLabel.setText("Please click button" +
                // gui.buttonCounter);

                gui.statusLabel.setText("what is " + buttonCounter + "+1");

            } else {
                gui.reset();
                gui.statusLabel
                        .setText("Incorrect button clicked, start again: 0 + 1");
            }

        }
    }
}

Updated with winner condition

if (clickedNumber == (gui.buttonCounter + 1)) {
    if (gui.buttonCounter == (gui.ROWS * gui.COLUMNS) - 1) {
        JOptionPane.showMessageDialog(null, "You Win", "Winner", JOptionPane.INFORMATION_MESSAGE);
    } else {
        gui.buttonCounter++;
        buttonClicked.setText("");// optional - clears correct selection
        if (gui.buttonCounter > gui.ROWS * gui.COLUMNS) {
            gui.reset();
        }
        // gui.statusLabel.setText("Please click button" +
        // gui.buttonCounter);

        gui.statusLabel.setText("what is " + buttonCounter + "+1");
    }
} else {
    gui.reset();
    gui.statusLabel
            .setText("Incorrect button clicked, start again: 0 + 1");
}

Upvotes: 3

Related Questions