Java- 调试 if-else 语句



我已经弄清楚我的代码几个小时了,但我找不到任何代码的解决方案 当我点击我的水、火或自然按钮时,游戏 GUI 不应该再显示,因为每个按钮都会添加 1 个以存储到我的游戏中,最大最多只能存储 5 次,我真的想不通,请帮帮我!真的很感激。

public class Game {
private JPanel Game;
private JButton Water;
private JButton Nature;
private JButton Fire;
private int myChoice;
private int computerChoice;
int gamePlayed = 0; //store the times of game played
public Game() {
JFrame frame = new JFrame("The Elements");
frame.setContentPane(this.Game);
frame.setMinimumSize(new Dimension(500, 500));
frame.pack();
frame.setVisible(true);

if (gamePlayed <= 5) {  //if the times of game played is less than 5 times , execute the following code
Water.addActionListener(new ActionListener() {
@Override
//Water counter Fire , Nature counter water
public void actionPerformed(ActionEvent e) {
int select;
select = JOptionPane.showConfirmDialog(null,
"You're using the power of water", "Water",
JOptionPane.YES_NO_OPTION);
if (select == JOptionPane.YES_OPTION) {
myChoice = 0;
computerChoice = computerPlays();
conditionsDisplayResults(myChoice, computerChoice);
gamePlayed+=1;//add 1 time of played game when the yes option clicked
}
}
});
Fire.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int select = JOptionPane.showConfirmDialog(null,
"You're using the power of fire", "Fire",
JOptionPane.YES_NO_OPTION);
if (select == JOptionPane.YES_OPTION) {
myChoice = 1;
computerChoice = computerPlays();
conditionsDisplayResults(myChoice, computerChoice);
gamePlayed+=1;//add 1 time of played game when the yes option clicked
}
}
});
Nature.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int select = JOptionPane.showConfirmDialog(null,
"You're using the power of nature", "Nature",
JOptionPane.YES_NO_OPTION);
if (select == JOptionPane.YES_OPTION) {
myChoice = 2;
computerChoice = computerPlays();
conditionsDisplayResults(myChoice, computerChoice);
gamePlayed+=1;//add 1 time of played game when the yes option clicked
}
}
});

else{ //else which when the times of game played is more than 5 times , execute the following code
GameResult();
MainMenu mainMenu = new MainMenu();
}
}

您的状况if (gamePlayed <= 5)测试一次:在程序开始时。 当您的一个按钮将 +1 添加到gamePlayed时,您只是不会在初始状态下再次出现。

有很多可能性可以解决这个问题,最简单的方法是在按钮单击事件中退出程序,例如:

public class Game
{
private JPanel  Game;
private JButton Water;
private JButton Nature;
private JButton Fire;
private int         myChoice;
private int         computerChoice;
int                         gamePlayed  = 0;    //store the times of game played
public Game()
{
JFrame frame = new JFrame( "The Elements" );
frame.setContentPane( this.Game );
frame.setMinimumSize( new Dimension( 500, 500 ) );
frame.pack();
frame.setVisible( true );
Water.addActionListener( new ActionListener()
{
@Override
//Water counter Fire , Nature counter water
public void actionPerformed( ActionEvent e )
{
int select;
select = JOptionPane.showConfirmDialog( null, "You're using the power of water", "Water", JOptionPane.YES_NO_OPTION );
if ( select == JOptionPane.YES_OPTION )
{
myChoice = 0;
computerChoice = computerPlays();
conditionsDisplayResults( myChoice, computerChoice );
gamePlayed += 1;//add 1 time of played game when the yes option clicked
}
if ( this.gamePlayed > 5 )
{
exit();
}
}
} );
Fire.addActionListener( new ActionListener()
{
@Override
public void actionPerformed( ActionEvent e )
{
int select = JOptionPane.showConfirmDialog( null, "You're using the power of fire", "Fire", JOptionPane.YES_NO_OPTION );
if ( select == JOptionPane.YES_OPTION )
{
myChoice = 1;
computerChoice = computerPlays();
conditionsDisplayResults( myChoice, computerChoice );
gamePlayed += 1;//add 1 time of played game when the yes option clicked
}
if ( this.gamePlayed > 5 )
{
exit();
}
}
} );
Nature.addActionListener( new ActionListener()
{
@Override
public void actionPerformed( ActionEvent e )
{
int select = JOptionPane.showConfirmDialog( null, "You're using the power of nature", "Nature", JOptionPane.YES_NO_OPTION );
if ( select == JOptionPane.YES_OPTION )
{
myChoice = 2;
computerChoice = computerPlays();
conditionsDisplayResults( myChoice, computerChoice );
gamePlayed += 1;//add 1 time of played game when the yes option clicked
}
if ( this.gamePlayed > 5 )
{
exit();
}
}
} );
}
void exit()
{
// exit code
}
}

最新更新