添加两个整数不做任何事情

Adding two ints not doing anything

所以问题是,当我说 ovrxp = ovrxp + xp 时,它永远不会叠加,只会重置每次击杀。解决此问题并解释为什么这不起作用将不胜感激。

@EventHandler
public void onDeath(EntityDeathEvent e) {

    Player player = (Player) e.getEntity().getKiller();
    Skeleton s = (Skeleton) e.getEntity();

    int ovrlvl = 1;
    int ovrxp = 0;

    Random random = new Random();
    int xp = random.nextInt(30) + 21;

    if (ovrlvl == 1 && ovrxp >= 200) {
        player.sendMessage(ChatColor.GREEN + "You are now level two!");
        player.playSound(player.getLocation(), Sound.LEVEL_UP, 1.0F, 0.0F);
        ovrlvl = 2;
    }
    if (ovrlvl == 2 && ovrxp >= 400) {
        player.sendMessage(ChatColor.GREEN + "You are now level three!");
        player.playSound(player.getLocation(), Sound.LEVEL_UP, 1.0F, 0.0F);
        ovrlvl = 3;
    }

    ovrxp = ovrxp + xp;

    if (s.getCustomName() == "Undead Recruit") {
        if (ovrlvl == 1) {
            player.sendMessage(ChatColor.GREEN + "" + ovrxp + "/200");
        }
        if (ovrlvl == 2) {
            player.sendMessage(ChatColor.GREEN + "" + ovrxp + "/400");
        }
    }
}
}

您已将 ovrxp 声明为 local 变量 - 每次调用 onDeath 时都会对其进行初始化。

如果您希望该值在对该方法的多次调用之间保持不变,您需要将变量设为一个字段(对象本身的一部分)。假设该方法总是在同一个对象上调用,并且在同一个线程上,只需将其设为实例字段就可以了。