JS while 循环不会在 true 处停止
JS while loop doesnt stop at true
当你运行这个脚本时,当你按 1 并单击 enter 时,它会显示两个口袋妖怪的生命值,它将你的攻击生命值减去敌人的生命值。当您或 ememy 命中 0 或少于 0 命中点时,它应该停止并仅在控制台日志中显示谁赢了。相反,它需要额外点击才能显示消息。
因此,如果您的马力为 -10,则需要多打一击。
let firstFight = false;
while (!firstFight) {
let fightOptions = prompt("1. Fight, 2.Items, 3.Potions " + wildPokemon[0].name + ":" + wildPokemon[0].hp + " " + pokeBox[0].name + ":" + pokeBox[0].hp);
if (fightOptions == 1) {
if (!firstFight) {
if (wildPokemon[0].hp <= 0) {
console.log("You have won!");
firstFight = true;
} else {
let attack1 = wildPokemon[0].hp -= pokeBox[0].attack.hp;
console.log(wildPokemon[0].hp);
}
if (pokeBox[0].hp <= 0) {
console.log(wildPokemon[0] + " has killed you");
firstFight = true;
} else {
let attack2 = pokeBox[0].hp -= wildPokemon[0].attack.hp;
console.log(pokeBox[0].hp);
}
}
} else if (fightOptions == 2) {
} else if (fightOptions == 3) {
} else {
}
}
有什么方法可以使这段代码更有效率吗?
当条件为假时循环停止,在你的情况下,你将它设置为不假,它不会停止,因为你没有明确地确定它。您可以通过两种方式进行操作。
第一名:
while(!firstFight == false)
第二名:
var firstFight = true;
同时(第一次战斗)
然后在 if else 语句中将 firstFight 设置为 false。
问题是,在您检查分数是否等于或小于零后,分数会被减去。这是您之前可以检查的方法:
let firstFight = false;
while (!firstFight) {
let fightOptions = prompt("1. Fight, 2.Items, 3.Potions " + wildPokemon[0].name + ":" + wildPokemon[0].hp + " " + pokeBox[0].name + ":" + pokeBox[0].hp);
if (fightOptions == 1) {
wildPokemon[0].hp -= pokeBox[0].attack.hp;
if (wildPokemon[0].hp <= 0) {
console.log("You have won!");
firstFight = true;
} else {
console.log(wildPokemon[0].hp);
}
pokeBox[0].hp -= wildPokemon[0].attack.hp;
if (!firstFight && pokeBox[0].hp <= 0) {
console.log(wildPokemon[0] + " has killed you");
firstFight = true;
} else {
console.log(pokeBox[0].hp);
}
} else if (fightOptions == 2) {
} else if (fightOptions == 3) {
} else {
}
}
你可以简单地添加另一个 if 条件来检查玩家的生命是否仍然大于 '0' 或小于 '0' 在同一个回合中像这样。
这样你就不必去下一回合检查玩家的生命加上它摆脱了额外的条件语句...
if (fightOptions == 1) {
let attack1 = wildPokemon[0].hp -= pokeBox[0].attack.hp;
console.log(wildPokemon[0].hp);
if (wildPokemon[0].hp <= 0) {
console.log("You have won!");
firstFight = true;
}
if (!firstFight){
let attack2 = pokeBox[0].hp -= wildPokemon[0].attack.hp;
console.log(pokeBox[0].hp);
if (pokeBox[0].hp <= 0) {
console.log(wildPokemon[0] + " has killed you");
firstFight = true;
}
}
}
当你运行这个脚本时,当你按 1 并单击 enter 时,它会显示两个口袋妖怪的生命值,它将你的攻击生命值减去敌人的生命值。当您或 ememy 命中 0 或少于 0 命中点时,它应该停止并仅在控制台日志中显示谁赢了。相反,它需要额外点击才能显示消息。
因此,如果您的马力为 -10,则需要多打一击。
let firstFight = false;
while (!firstFight) {
let fightOptions = prompt("1. Fight, 2.Items, 3.Potions " + wildPokemon[0].name + ":" + wildPokemon[0].hp + " " + pokeBox[0].name + ":" + pokeBox[0].hp);
if (fightOptions == 1) {
if (!firstFight) {
if (wildPokemon[0].hp <= 0) {
console.log("You have won!");
firstFight = true;
} else {
let attack1 = wildPokemon[0].hp -= pokeBox[0].attack.hp;
console.log(wildPokemon[0].hp);
}
if (pokeBox[0].hp <= 0) {
console.log(wildPokemon[0] + " has killed you");
firstFight = true;
} else {
let attack2 = pokeBox[0].hp -= wildPokemon[0].attack.hp;
console.log(pokeBox[0].hp);
}
}
} else if (fightOptions == 2) {
} else if (fightOptions == 3) {
} else {
}
}
有什么方法可以使这段代码更有效率吗?
当条件为假时循环停止,在你的情况下,你将它设置为不假,它不会停止,因为你没有明确地确定它。您可以通过两种方式进行操作。
第一名:
while(!firstFight == false)
第二名:
var firstFight = true; 同时(第一次战斗) 然后在 if else 语句中将 firstFight 设置为 false。
问题是,在您检查分数是否等于或小于零后,分数会被减去。这是您之前可以检查的方法:
let firstFight = false;
while (!firstFight) {
let fightOptions = prompt("1. Fight, 2.Items, 3.Potions " + wildPokemon[0].name + ":" + wildPokemon[0].hp + " " + pokeBox[0].name + ":" + pokeBox[0].hp);
if (fightOptions == 1) {
wildPokemon[0].hp -= pokeBox[0].attack.hp;
if (wildPokemon[0].hp <= 0) {
console.log("You have won!");
firstFight = true;
} else {
console.log(wildPokemon[0].hp);
}
pokeBox[0].hp -= wildPokemon[0].attack.hp;
if (!firstFight && pokeBox[0].hp <= 0) {
console.log(wildPokemon[0] + " has killed you");
firstFight = true;
} else {
console.log(pokeBox[0].hp);
}
} else if (fightOptions == 2) {
} else if (fightOptions == 3) {
} else {
}
}
你可以简单地添加另一个 if 条件来检查玩家的生命是否仍然大于 '0' 或小于 '0' 在同一个回合中像这样。
这样你就不必去下一回合检查玩家的生命加上它摆脱了额外的条件语句...
if (fightOptions == 1) {
let attack1 = wildPokemon[0].hp -= pokeBox[0].attack.hp;
console.log(wildPokemon[0].hp);
if (wildPokemon[0].hp <= 0) {
console.log("You have won!");
firstFight = true;
}
if (!firstFight){
let attack2 = pokeBox[0].hp -= wildPokemon[0].attack.hp;
console.log(pokeBox[0].hp);
if (pokeBox[0].hp <= 0) {
console.log(wildPokemon[0] + " has killed you");
firstFight = true;
}
}
}