有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在方法中重置java变量

我正在尝试创建一个简单的程序,它有一个“玩家”和一个“怪物”,并随机抽取数字来降低生命值。然后我希望它打印在一个文本区域。除FightTheMonster方法外,我的变量monster和player在任何地方都被重置为0。(很抱歉代码太草率了)。当我在playerDied或MonsterDead方法中打印怪物或玩家时,无论发生什么情况,它们都打印为0,这在主方法中也会发生

import java.util.Random;
import java.awt.*;
import javax.swing.*;

public class Main extends JPanel {

    JTextArea textArea;
    static final String NEWLINE = System.getProperty("line.separator");
    public static int player;
    public static int monster;
    private static void CreateAndShowGui() {
        JFrame frame = new JFrame("Monster Fight");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JComponent newContentPane = new Main();
        newContentPane.setOpaque(true);
        frame.setContentPane(newContentPane);
        frame.pack();
        frame.setVisible(true);
    }
public static void FightTheMonster() {
    boolean loop = true;
    Main.player=10;
    Main.monster=10;

    while (loop == true) {
        Random rand = new Random();
        int n = rand.nextInt(3) + 1;
        Random monsterroll = new Random();
        int m = monsterroll.nextInt(3) + 1;
        player = player - n;
        monster = monster - m;
        System.out.println("player health " + player + " monsterhealth " + monster);

        if(player <= 0) {
            loop = false;
            System.out.println("Player Died");
        }
        if(monster <= 0) {
            loop = false;
            System.out.println("Monster Died");

        }
    }
}


public void monsterDied(int monster) {
    Main.monster = Main.monster;
    System.out.println("monster health in void" + monster);
    if(monster <= 0) {
    textArea.append("Monster Died");
    System.out.println("Monster Died in void");
    }
}
public void playerDied(int player) {
    if(player <= 0) {
    textArea.append("Player Died");
    System.out.println("Player Died in void");
    }
}
public Main() {
    super(new GridLayout(0,1));
    textArea = new JTextArea();
    textArea.setEditable(false);
    JScrollPane scrollPane = new JScrollPane(textArea);
    scrollPane.setVerticalScrollBarPolicy(
            JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    scrollPane.setPreferredSize(new Dimension(200, 75));
    add(scrollPane);
}

public static void main(String[] args) {
    Main.monster = Main.monster;
    System.out.println("Monster Health in Main Method " + monster);
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            CreateAndShowGui();
            FightTheMonster();

        }
    });
    Main juice = new Main();
    juice.playerDied(player);
    juice.monsterDied(Main.monster);
        }


}

共 (2) 个答案

  1. # 1 楼答案

    定义怪物和玩家的静态字段时,请给出它们的值:

     public static int player=10;
        public static int monster=10;
    

    因为fight方法在信息字符串之后运行:System.out.println("Monster Health in Main Method " + monster);将打印默认值零

  2. # 2 楼答案

    这里发生的主要问题是线程/计时问题。您正在一个新线程上调用CreateAndShowGui和FightTheMonster方法,并使用“InvokeLater”运行它们。如果您查看控制台输出,您会注意到这是在juice之后发生的。游戏和果汁。有人在叫怪物。以下是我在运行代码时看到的输出:

    Monster Health in Main Method 0

    Player Died in void

    monster health in void0

    Monster Died in void

    player health 8 monsterhealth 7

    player health 7 monsterhealth 5

    player health 6 monsterhealth 3

    player health 4 monsterhealth 2

    player health 1 monsterhealth 0

    Monster Died

    这显示了事情发生的顺序。因此,在将变量设置为10之前,将调用playerDied和monsterDied方法

    我不是swing专家,也不确定您的程序背后的想法是什么,但您可以尝试使用invokeAndWait而不是invokeLater来解决这个问题。这将导致方法按照您在主方法中列出的顺序发生

    也就是说,作为一个额外的改进,我可能会用player和monster之类的东西创建新的类,在main方法中实例化它们,将它们传递到fightTheMonster方法中,而不是像现在这样使用静态变量