gpt4 book ai didi

java在事件发生前单击2个按钮

转载 作者:太空宇宙 更新时间:2023-11-04 10:40:04 24 4
gpt4 key购买 nike

我是 Java 新手,想要创建一个小型骰子游戏。在游戏中,您掷两个骰子,然后根据两个骰子相加或相减的值,单击 1 到 9 之间的按钮。这工作正常除了,当我滚动 6 和 6 时,我希望能够单击 0(用于减法,有效)或同时单击 1 和 2(用于加 12)。我只能为单值按钮点击编写代码,而不能为 1 和 2 编写代码。如果我掷出 6 和 6,如何能够单击 1 和 2 有什么想法吗? (下面的代码仅适用于按钮 0、1、2)感谢您对此的任何帮助。

package dice;
import java.awt.*;
import java.awt.event.*;
import static java.lang.Math.abs;
import javax.swing.JOptionPane;

public class Dice extends Frame implements WindowListener,ActionListener {
TextField text = new TextField(20);
TextField dice1 = new TextField(10);
TextField dice2 = new TextField(10);
TextField plusvalue = new TextField(10);
TextField minusvalue = new TextField(10);
Button b0;
Button b1;
Button b2;
Button nomatch;
Button rolldice;
private int numClicks = 0;
int dice1value;
int dice2value;
int SIDES = 6;
int plus;
int minus;
public void roll() {
//roll dice
dice1value = (int) (Math.random() * SIDES) + 1;
dice2value = (int) (Math.random() * SIDES) + 1;
plus = dice1value + dice2value;
minus = abs(dice1value - dice2value);
dice1.setText("Dice 1 = "+ Integer.toString(dice1value));
dice2.setText("Dice 2 = "+Integer.toString(dice2value));
plusvalue.setText("Addition = "+Integer.toString(plus));
minusvalue.setText("Subtract = "+Integer.toString(minus));
}
public static void main(String[] args) {
Dice myWindow = new Dice("My Dice Game");
myWindow.setSize(400,400);
myWindow.setVisible(true);

}

public Dice(String title) {

super(title);
setLayout(new FlowLayout());
addWindowListener(this);
rolldice = new Button("Roll Dice");
b0 = new Button("0");
b1 = new Button("1");
b2 = new Button("2");
nomatch = new Button("No Match");
add(b0);
add(b1);
add(b2);
add(rolldice);
add(text);
add(dice1);
add(dice2);
add(plusvalue);
add(minusvalue);
add(nomatch);
b0.addActionListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
nomatch.addActionListener(this);
rolldice.addActionListener(this);
}

public void actionPerformed(ActionEvent e) {

if (e.getSource()==b0)
{
if(plus==0||minus==0)
{ numClicks++;
text.setText("Button Clicked " + numClicks + " times");
b0.setEnabled(false);
} else { JOptionPane.showMessageDialog(null, "You can't do that", "Error",
JOptionPane.ERROR_MESSAGE);}}
else if (e.getSource()==b1)
{ if(plus==1||minus==1)
{ numClicks++;
text.setText("Button Clicked " + numClicks + " times");
b1.setEnabled(false);
} else { JOptionPane.showMessageDialog(null, "You can't do that", "Error",
JOptionPane.ERROR_MESSAGE);}}
else if (e.getSource()==b2)
{
if(plus==2||minus==2)
{ numClicks++;
text.setText("Button Clicked " + numClicks + " times");
b2.setEnabled(false);
} else { JOptionPane.showMessageDialog(null, "You can't do that", "Error",
JOptionPane.ERROR_MESSAGE);}}
else if (e.getSource()==nomatch)
{
numClicks++;
text.setText("Button Clicked " + numClicks + " times");
}
else if (e.getSource()==rolldice)
{roll();


}

}

public void windowClosing(WindowEvent e) {
dispose();
System.exit(0);
}

public void windowOpened(WindowEvent e) {}
public void windowActivated(WindowEvent e) {}
public void windowIconified(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}
}

最佳答案

最简单的方法是在玩家点击数字后添加另一个按钮,例如“完成”。

当玩家单击数字时,将按钮值附加到字符串中。因此,当单击按钮“1”时,字符串将保存“1”。如果玩家再次单击按钮“2”,字符串将变为“12”。

当玩家单击“完成”按钮时,将字符串解析为整数并与预期答案进行比较,并将字符串重置为空。

如果您不想实现“完成”按钮,则每次单击按钮时,将计数变量加 1。如果计数为 2,则触发检查玩家的答案并将计数重置为 0。

关于java在事件发生前单击2个按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49090968/

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