【发布时间】:2019-09-27 14:30:37
【问题描述】:
我正在构建一个迷你游戏,其中两个怪物互相战斗(一个对手和玩家)。当回合开始时,怪物应该战斗并且他们的健康点减少1、2、3、4或5。
我正在使用 Math.random 来随机化对每个怪物造成的伤害。
程序运行和回合开始时如何减少每个怪物的生命值?
这是我目前的代码(Monster.java 文件):
import java.util.Random;
public class Monster {
// Monster properties
private String name;
private int health;
private int damage;
// Random damage points that vary from 1 to 5
int randomDamagePoints = (int)(Math.random() * 1 + 5);
// Constructor
public Monster(String name, int health, int damage) {
this.name = "Monster";
this.health = 50;
this.damage = 2;
}
// Opponent attacks Monster 1 - Player
public void AttackPlayer(Monster player) {
while(health > 0) {
// Part I need help with
}
}
// Player attacks Monster 2 - Opponent
public void AttackOpponent(Monster opponent) {
while(health > 0) {
// Part I need help with
}
}
}
感谢您的帮助!
【问题讨论】:
-
有什么问题?将
int randomDamagePoints = (int)(Math.random() * 1 + 5);放入函数AttackOpponent并返回计算值并从健康或其他东西中减去它。 -
另外,您现在实际上并没有使用他们的
damagestat。例如,您可以生成一个随机值,例如 1 到 5,然后将其乘以它们的伤害,这样您就可以调整怪物的强度。例如,使用更大的数字可能会很有用,以免强度从1到3(或小数)。