【发布时间】:2017-04-29 20:54:27
【问题描述】:
这就是我想要做的:
两个对象有 hp 和 power 变量。我想在他们之间进行一场战斗。逻辑是做一个这样的循环:object1HP-object2Power,Object2HP-Object2Power。当其中一个物体的生命值为 0 或以下时 - 打印谁赢了。
这是我目前所拥有的:
this.battle = function(other) {
do {
this.hp - other.power;
other.hp - this.power;
}
while (this.hp <=0 || other.hp <=0);
if(this.hp <=0) {
console.log(this.name + " won!");
} else {
console.log(other.name + " won!");
}
}
我知道这可能是一团糟。谢谢!
【问题讨论】:
-
您需要将循环更改为 and (&&) and > 0,这样它就会一直持续到一个小于或等于零为止
标签: javascript loops object while-loop