if else语句不会工作java

时间:2014-04-10 02:05:55

标签: java swing

问题字段应包含36个问题,这些问题的答案在网格中的36个按钮上。

当用户选择单击正确的按钮时,按钮从网格中取出,目的是清除网格,使网格为空。

如果用户点击不正确的按钮,游戏将重新启动。

我在添加问题标签方面遇到了问题。

当用户点击正确的按钮时,问题字段应显示36个问题,从0 + 1开始,然后显示 问题2在字段中显示的是1 + 1,直到问题36

我试图通过更改此代码行来获得此功能

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

到此:

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

但游戏无法正常运行,但选择了正确的答案,代码一直在说"否则"在这个代码即。单击不正确的按钮,再次启动: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");
    }

    }
}

我该如何解决这个问题?因此,当我单击正确的答案按钮时,它会移动到下一个问题,并且不会显示单击不正确的按钮,再次启动:0 + 1是什么。

完整代码

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");
            }

        }
    }
}

1 个答案:

答案 0 :(得分:3)

所以,从您的previous question获取代码并添加

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

进入actionPerformed方法询问......

&#34;什么是0 + 1 1&#34; (应该是&#34;什么是0 + 1&#34;)。我点击&#34; 1&#34;,输出......

ClickedNumber = 1
gui.buttonCounter = 1

所以查看if语句;

if (clickedNumber == gui.buttonCounter) {

这是true ...

然后它问......

&#34;什么是2 + 1&#34;。我点击&#34; 3&#34;,输出......

ClickedNumber = 3
gui.buttonCounter = 2

所以查看if语句;

if (clickedNumber == gui.buttonCounter) {

哪个是false

您的代码中存在逻辑错误。基本上,它实际上不知道问题是什么......

可运行的示例

基本上这是做什么的......

  

&#34;什么是&#34; + buttonCounter +&#34; + 1&#34;?

if条件下,我们假设buttonCounter已被0编入索引,这意味着我们需要将比较增加1

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

或者您可以将clickedNumeber减少1

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

请记住,你的问题总是要求&#34;下一个号码是什么&#34;而不是当前的号码...

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");
            }

        }
    }
}

更新了获胜者条件

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");
}