Java - 存储更改的int变量

时间:2017-11-16 11:31:40

标签: java

所以我正在尝试编写最简单的游戏,每次“怪物”攻击“玩家”我希望变量“int totalHealth”降低。

public void attack(Player somePlayer) {

    int totalHealth = somePlayer.getHitPoints() + somePlayer.getStrength();

    int remainingHealth = totalHealth - damage;
    if (remainingHealth == 0 || remainingHealth < 0) {
        System.out.println("Your player died");
    } else {
        System.out.println("The monster attacked " + somePlayer.getName() + " and made " + this.damage + " damage");
        System.out.println("Your remaining health is - " + (remainingHealth - somePlayer.getStrength()));
    }

}

但问题是变量“remainingHealth”保持不变,只是我第一次运行它降低的代码时,每次下次保持相同时,我想问题就在这一行:

int totalHealth = somePlayer.getHitPoints() + somePlayer.getStrength();

我猜每次运行代码

somePlayer.getHitpoints() 

从构造函数中获取插入的整数,这就是问题所在。

我需要找出以某种方式将剩余健康状况存储在变量中的方法

玩家类:

public class Player implements ISavable{

private String name;
private int hitPoints ;
private int strength ;
private String weapon;
private int damage;
private int totalHealth = hitPoints + strength;

public Player(String name, int damage , int hitPoints , int strength) {
    this.name = name;
    this.damage = damage;
    this.weapon = "Sword";
    this.hitPoints = hitPoints;
    this.strength = strength;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getHitPoints() {
    return hitPoints;
}

public void setHitPoints(int hitPoints) {
    this.hitPoints = hitPoints;
}

public int getStrength() {
    return strength;
}

public void setStrength(int strength) {
    this.strength = strength;
}

public String getWeapon() {
    return weapon;
}

public void setWeapon(String weapon) {
    this.weapon = weapon;
}

public int getTotalHealth() {
    return totalHealth;
}

public void setTotalHealth(int totalHealth) {
    this.totalHealth = totalHealth;
}

@Override
public String toString() {
    return "Player{" +
            "name='" + name + '\'' +
            ", hitPoints=" + hitPoints +
            ", strength=" + strength +
            ", weapon='" + weapon + '\'' +
            '}';
}

@Override
public List<String> write() {
    List<String> values = new ArrayList<String>();
    values.add(0, this.name);
    values.add(1, "" + this.hitPoints);
    values.add(2, "" + this.strength);
    values.add(3, "" + this.weapon);
    values.add(4,"" + this.damage);
    return values;
}

@Override
public void read(List<String> savedValues) {
    if (savedValues != null && savedValues.size()>0){
        this.name = savedValues.get(0);
        this.hitPoints = Integer.parseInt(savedValues.get(1));
        this.strength = Integer.parseInt(savedValues.get(2));
        this.weapon = savedValues.get(3);
    }
}

public void attack(Monster someMonster){
    int health = someMonster.getHitPoints();
    int remainingHealth = health - damage;
    if (remainingHealth == 0 || remainingHealth < 0) {
        System.out.println("You killed the monster !!!");
    } else {
        System.out.println("You attacked the monster " + " and made " + this.damage + " damage");
        System.out.println("Monsters remaining health is - " + remainingHealth);
    }

    if (someMonster.isDead()){
        this.hitPoints = 100;
        this.strength = 50;
    }
}

public void healPlayer(Player somePlayer){
    int hp = somePlayer.getHitPoints();
    hp += 10;
    System.out.println("You healed the player for 10hp , your current hp is " + hp);
}

}

3 个答案:

答案 0 :(得分:1)

您没有更新totalHealth的值,每次调用attack()方法时都会对其进行初始化,因此您始终拥有相同的Health。

你可以通过以下行解决它的攻击()方法:

int totalHealth = somePlayer.getHitPoints() + somePlayer.getStrength();

修改

public void attack(Player somePlayer) {

    somePlayer.totalHealth = somePlayer.totalHealth - this.damage;
    if (somePlayer.totalHealth.equals(0) || somePlayer.totalHealth < 0) {
        System.out.println("Your player died");
    } else {
        System.out.println("The monster attacked " + somePlayer.getName() + " and made " + this.damage + " damage");
        System.out.println("Your remaining health is - " + (somePlayer.totalHealth - somePlayer.getStrength()));
    }

}

答案 1 :(得分:0)

Player上创建一个函数updateHealth(int newHealth),用于设置从getHitPoints()返回到newHealth的变量。然后,只需在somePlayer.updateHealth(somePlayer.getHitPoints() - damage);函数的末尾添加attack

答案 2 :(得分:0)

你应该有一个健康的表示,在玩家totalHealth中定义。

你可以使用一种计算总生命值的方法(因为你的玩家可以得到一个增加力量的物品或一个能够治愈你的物品)。

然后你应该在Player中使用另一个变量,remainingHealth并将其初始化为remainingHealth = totalHealth

    public class Player {

    float totalHealth;
    float remainingHealth;
    ...

    public Player(...) {
        this.remainingHealth = this.totalHealth;
        ...
    }
    ...

}

当你想降低玩家的健康状况时,你应该使用remainingHealth。

    public void attack(Player somePlayer) {

    somePlayer.remainingHealth -= this.damage;
    if (somePlayer.remainingHealth <= 0) {
        System.out.println("Your player died");
    } else {
        System.out.println("The monster attacked ...");
        System.out.println("Your remaining health is - " + somePlayer.remainingHealth);
    }

}