程序在玩家1的生命值降为0时不会结束。

huangapple go评论88阅读模式
英文:

The program doesn't end when player 1 life reaches 0

问题

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Swing16 implements ActionListener {
    static int life1 = 100;
    static int life2 = 100;
    JLabel AttRig;
    JFrame jfrm;  // Declare the frame as a class variable

    Swing16() {
        // Initialize the frame settings
        jfrm = new JFrame("game");  // Use class variable for frame
        jfrm.setLayout(new FlowLayout());
        jfrm.setSize(500, 500);
        jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // ... (rest of your code)

        JButton restartButton = new JButton("Restart Game");  // Add restart button
        restartButton.addActionListener(this);  // Add action listener
        jfrm.add(restartButton);  // Add restart button to frame

        // ... (rest of your code)

    }

    // ... (rest of your code)

    // Cosa succede se i bottoni vengono cliccati.
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("Attack player two (-25)")) {
            // ... (rest of your code)
        } else if (e.getActionCommand().equals("Regenerate player one (+15)")) {
            // ... (rest of your code)
        } else if (e.getActionCommand().equals("Attack player one (-25)")) {
            // ... (rest of your code)
        } else if (e.getActionCommand().equals("Regenerate player two (+15)")) {
            // ... (rest of your code)
        } else if (e.getActionCommand().equals("Restart Game")) {  // Handle restart button
            life1 = 100;
            life2 = 100;
            AttRig.setText("Player 1 life: " + life1 + " | " + "Player 2 life: " + life2);
        } else if (life1 < 0) {
            // ... (rest of your code)
        } else if (life2 < 0) {
            // ... (rest of your code)
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new Swing16();
            }
        });
    }
}

I've added a "Restart Game" button to your program, and when this button is pressed, it resets the life values of both players to 100 and updates the label text accordingly.

英文:

I'm making a little game using the Java Swing, in which there are four bottoms: attack player 1, attack player 2, regenerate player 1, regenerate player 2. When attack player 1 or attack player 2 push bottom are pressed, it removes -25 from the life of the player, vice versa, when regenerate bottoms are pressed it adds +15 to player's life. Now, everything works perfectly here, but when life reaches zero, the player is dead and the label text changes in: "Player 1 is dead" or "Player2 is dead", but this never happens:

程序在玩家1的生命值降为0时不会结束。

as you can see from the picture, player 1 life has been incremented and player 2 life even reached -50, this is the code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Swing16 implements ActionListener {
static int life1 = 100;
static int life2 = 100;
JLabel AttRig;
Swing16() {
// Imposta le impostazioni del frame
JFrame jfrm = new JFrame(&quot;game&quot;);
// Layout basico
jfrm.setLayout(new FlowLayout());
// Grandezza del frame (500 x 500).
jfrm.setSize(500, 500);
// Esci dal programma se l&#39;utente esce dal frame.
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton attacca1  = new JButton(&quot;Attack player one (-25)&quot;); //Attack player two (-25)
JButton rigenera1 = new JButton(&quot;Attack player two (-25)&quot;); // Regenerate player one (+15)
JButton attacca2  = new JButton(&quot;Regenerate player one (+15)&quot;); // Attack player one (-25)
JButton rigenera2 = new JButton(&quot;Regenerate player two (+15)&quot;);
// Aggiungi action listeners per i push bottom.
attacca1.addActionListener(this);
attacca2.addActionListener(this); // rigenera1.addActionListener(this);
rigenera1.addActionListener(this);
rigenera2.addActionListener(this);
// Aggiung pulsanti.
jfrm.add(attacca1);
jfrm.add(attacca2);
jfrm.add(rigenera1);
jfrm.add(rigenera2);
// Nuovo label.
AttRig = new JLabel(&quot;Player 1 life: 100 | Player 2 life: 100&quot;);
// Aggiungi label al frame 
jfrm.add(AttRig);
// Visibilit&#224; del frame 
jfrm.setVisible(true);
}
// Cosa succede se i bottoni vengono cliccati.
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals(&quot;Attack player two (-25)&quot;)) {
life2 = life2 - 25;
AttRig.setText(&quot;Player 1 life: &quot; + life1 + &quot; | &quot; + &quot;Player 2 life: &quot; + life2);
}
else if(e.getActionCommand().equals(&quot;Regenerate player one (+15)&quot;)){
life1 = life1 + 15;
AttRig.setText(&quot;Player 1 life: &quot; + life1 + &quot; | &quot; + &quot;Player 2 life: &quot; + life2);
}
else if(e.getActionCommand().equals(&quot;Attack player one (-25)&quot;)) {
life1 = life1 - 25;
AttRig.setText(&quot;Player 1 life: &quot; + life1 + &quot; | &quot; + &quot;Player 2 life: &quot; + life2);
}
else if(e.getActionCommand().equals(&quot;Regenerate player two (+15)&quot;)) {
life2 = life2 + 15;
AttRig.setText(&quot;Player 1 life: &quot; + life1 + &quot; | &quot; + &quot;Player 2 life: &quot; + life2);
}	
else if(life1 &lt; 0) {
AttRig.setText(&quot;Player 1 dies...&quot;);
}
else if(life2 &lt; 0) {
AttRig.setText(&quot;Player 2 dies...&quot;);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Swing16();
}
});
}
}

what may I do make restart the program through another push bottom? Thank you very much.

答案1

得分: 1

你应该在修改生命值之后(!)检查生命值。

public void actionPerformed(ActionEvent e) 方法中,以下的 else if 部分应该分别改为两个独立的 if 语句,如下所示:

if (life1 < 0) {
    AttRig.setText("玩家 1 已阵亡...");
}
if (life2 < 0) {
    AttRig.setText("玩家 2 已阵亡...");
}

所以代码应该是这样的:

public void actionPerformed(ActionEvent e) {
    if (e.getActionCommand().equals("攻击玩家二 (-25)")) {
        life2 = life2 - 25;
        AttRig.setText("玩家 1 生命值: " + life1 + " | " + "玩家 2 生命值: " + life2);
    } else if (e.getActionCommand().equals("玩家一恢复 (+15)")) {
        life1 = life1 + 15;
        AttRig.setText("玩家 1 生命值: " + life1 + " | " + "玩家 2 生命值: " + life2);
    } else if (e.getActionCommand().equals("攻击玩家一 (-25)")) {
        life1 = life1 - 25;
        AttRig.setText("玩家 1 生命值: " + life1 + " | " + "玩家 2 生命值: " + life2);
    } else if (e.getActionCommand().equals("玩家二恢复 (+15)")) {
        life2 = life2 + 15;
        AttRig.setText("玩家 1 生命值: " + life1 + " | " + "玩家 2 生命值: " + life2);
    }   

    if (life1 < 0) {
        AttRig.setText("玩家 1 已阵亡...");
    }
    if (life2 < 0) {
        AttRig.setText("玩家 2 已阵亡...");
    }
}
英文:

You should check the life values after(!) you changed them.

In public void actionPerformed(ActionEvent e) method, the following else if part should be 2 separate if statements as follows:

        if(life1 &lt; 0) {
AttRig.setText(&quot;Player 1 dies...&quot;);
}
if(life2 &lt; 0) {
AttRig.setText(&quot;Player 2 dies...&quot;);
}

So it should look like this:

 public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals(&quot;Attack player two (-25)&quot;)) {
life2 = life2 - 25;
AttRig.setText(&quot;Player 1 life: &quot; + life1 + &quot; | &quot; + &quot;Player 2 life: &quot; + life2);
}
else if(e.getActionCommand().equals(&quot;Regenerate player one (+15)&quot;)){
life1 = life1 + 15;
AttRig.setText(&quot;Player 1 life: &quot; + life1 + &quot; | &quot; + &quot;Player 2 life: &quot; + life2);
}
else if(e.getActionCommand().equals(&quot;Attack player one (-25)&quot;)) {
life1 = life1 - 25;
AttRig.setText(&quot;Player 1 life: &quot; + life1 + &quot; | &quot; + &quot;Player 2 life: &quot; + life2);
}
else if(e.getActionCommand().equals(&quot;Regenerate player two (+15)&quot;)) {
life2 = life2 + 15;
AttRig.setText(&quot;Player 1 life: &quot; + life1 + &quot; | &quot; + &quot;Player 2 life: &quot; + life2);
}   
if(life1 &lt; 0) {
AttRig.setText(&quot;Player 1 dies...&quot;);
}
if(life2 &lt; 0) {
AttRig.setText(&quot;Player 2 dies...&quot;);
}
}

huangapple
  • 本文由 发表于 2020年10月12日 20:25:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/64317863.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定