Java 如何使用Jbutton在if条件为true或false时显示不同的注释
我想显示5个不同的意见时,按钮(回答Btn)被点击。 在这里,我们可以看到显示“正确”注释的JOption消息对话框 当我再次单击“回答”按钮时,我希望它显示另一条评论,如“很棒的工作” 这是我的ansBtnListener的代码Java 如何使用Jbutton在if条件为true或false时显示不同的注释,java,joptionpane,messagedialog,Java,Joptionpane,Messagedialog,我想显示5个不同的意见时,按钮(回答Btn)被点击。 在这里,我们可以看到显示“正确”注释的JOption消息对话框 当我再次单击“回答”按钮时,我希望它显示另一条评论,如“很棒的工作” 这是我的ansBtnListener的代码 class ansBtnListener implements ActionListener { public void actionPerformed(ActionEvent e) { // checking answer is correc
class ansBtnListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
// checking answer is correct or wrong
double doubleOfInput = Double.parseDouble(input.getText()); // getting string to double
if (doubleOfInput == result) {
JOptionPane.showMessageDialog(null, "Correct");
} else {
JOptionPane.showMessageDialog(null, "Wrong, Try Again!");
input.setText(" ");
}
}
}
非常感谢您的帮助。您可以添加一个方法局部变量,并在初始阶段将其赋值为零。因此,如果用户正在应答,那么第一次检查if子句中该变量的值。如果值为0,则显示“正确”并将值增加1,如果用户再次按下应答按钮,则将从变量的值中获得该值,并根据需要显示“出色”或“出色”
class ansBtnListener implements ActionListener {
int count = 0;
public void actionPerformed(ActionEvent e) {
// checking answer is correct or wrong
double doubleOfInput = Double.parseDouble(input.getText()); // getting string to double
if (doubleOfInput == result && count==0) {
JOptionPane.showMessageDialog(null, "Correct");
count++;
}
else if (doubleOfInput == result && count==1) {
JOptionPane.showMessageDialog(null, "Great Job!");
count++;
}
else if (doubleOfInput == result && count==2) {
JOptionPane.showMessageDialog(null, "Excellent!");
count++;
}
else if (doubleOfInput == result && count>2) {
JOptionPane.showMessageDialog(null, "Please click on end!");
}
else {
JOptionPane.showMessageDialog(null, "Wrong, Try Again!");
input.setText(" ");
}
}
}
您好,谢谢您,但是有没有办法对显示的消息进行洗牌?例如,如果第一个对话框是“正确的”,第二个对话框是“很棒的工作!”,第三个对话框是“优秀的”,我如何洗牌或循环该对话框,以便在单击回答按钮时随机显示?