gpt4 book ai didi

java - if else 语句不起作用 java

转载 作者:行者123 更新时间:2023-12-01 23:04:45 24 4
gpt4 key购买 nike

问题字段应包含 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");
}

}
}
}

最佳答案

因此,从您的 previous question 中获取代码并添加

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

进入它要求的actionPerformed方法...

“什么是 0+1 1”(应该是“什么是 0+1”)。我点击“1”,输出...

ClickedNumber = 1
gui.buttonCounter = 1

所以看看if语句;

if (clickedNumber == gui.buttonCounter) {

这是真实...

然后它会问...

“什么是2+1”。我点击“3”,输出...

ClickedNumber = 3
gui.buttonCounter = 2

所以看看if语句;

if (clickedNumber == gui.buttonCounter) {

这是错误

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

可运行示例

基本上,它的作用是询问...

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

if 条件中,我们假设 buttonCounter 的索引为 0,这意味着我们需要将比较增加 1

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

或者您可以将 clickedNumber 减少 1

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

记住,你的问题总是问“下一个数字是什么”,而不是当前的数字......

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

关于java - if else 语句不起作用 java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22977395/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com