gpt4 book ai didi

Java - 存储不断变化的 int 变量

转载 作者:行者123 更新时间:2023-12-02 12:00:07 24 4
gpt4 key购买 nike

所以我试图编写最简单的游戏,每次“怪物”攻击“玩家”时我都希望变量“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);
}

}

最佳答案

您没有更新totalHealth的值,而是在每次调用attack()方法时初始化它,因此您始终拥有相同的生命值。

您可以通过将 Attack() 方法放入该行来解决它:

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()));
}

}

关于Java - 存储不断变化的 int 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47328574/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com