gpt4 book ai didi

javascript - 为什么当我在最后一个 else 语句中更改 slaying 时,浏览器会崩溃

转载 作者:行者123 更新时间:2023-12-03 09:51:15 25 4
gpt4 key购买 nike

我在代码学院玩,出于某种原因,当我将最后一个杀戮更改为 true 时,我将 else 语句设置为 true,而不是使该人无敌,它只会使我的浏览器崩溃任何想法,为什么提前感谢您

 var slaying = true;
var youHit = Math.floor(Math.random()*2);
var damageThisRound = Math.floor(Math.random()* 5 + 1);
var totalDamage = 0;

while (slaying){

if (youHit)
{
console.log("how can you hit an emaginary creature");
totalDamage += damageThisRound;
if (totalDamage >= 4)
{
console.log("you slew the imagenary dragon");
slaying = false;
}
else
{
var youHit = Math.floor(Math.random()*2);
}
}
else
{
console.log("you defeated yourself because dragons dont exist");
slaying = true;//i changed it from false
}

}

最佳答案

while (condition) { }

只要条件为真,while 循环就会循环

if (condition) { } else { }

如果条件为falsy,则if else 语句将继续到else 语句。 .

在您的情况下,您有 youHit = false (或等于 0)和 slaying = true。由于 while 循环只要 slaying 为 true 就会循环,并且由于您不将 youHit 更改为 true,因此您最终将陷入无限循环,并且浏览器将愉快地一遍又一遍地执行相同的语句,永远不会脱离 while 循环.

关于javascript - 为什么当我在最后一个 else 语句中更改 slaying 时,浏览器会崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30877090/

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