Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
简单的JavaScript if/else语句_Javascript_If Statement - Fatal编程技术网

简单的JavaScript if/else语句

简单的JavaScript if/else语句,javascript,if-statement,Javascript,If Statement,我正在学习Javascript。这是我的密码。这是一个简单的程序,其中用户与一条龙战斗,但我添加了一个额外的位,如果龙将用户的健康降低到0,则代码完成。然而,每当我运行这段代码时,一旦龙开始降低用户的健康状况,这就是所有发生的事情。用户无法与龙交换打击。我做错了什么 var userHealth = 5; var youHit = Math.floor(Math.random() * 2); var damageThisRound = Math.floor(Math.random()*5 + 1

我正在学习Javascript。这是我的密码。这是一个简单的程序,其中用户与一条龙战斗,但我添加了一个额外的位,如果龙将用户的健康降低到0,则代码完成。然而,每当我运行这段代码时,一旦龙开始降低用户的健康状况,这就是所有发生的事情。用户无法与龙交换打击。我做错了什么

var userHealth = 5;
var youHit = Math.floor(Math.random() * 2);
var damageThisRound = Math.floor(Math.random()*5 + 1);
var totalDamage = 0;
var dragonDamage = Math.floor(Math.random() * 2);
while (userHealth > 0) {
    if (youHit) {
        console.log("You hit the dragon!");
        totalDamage += damageThisRound;
        console.log("Total damage dealt: " + totalDamage + "!");
        if (totalDamage >= 4) {
            console.log("You slew the dragon!");
            userHealth = 0;
        }
        else {
            youHit = Math.floor(Math.random() * 2);
        }
    }
    else {
        console.log("The dragon has dealt damage to you!");
        userHealth -= dragonDamage;
        dragonDamage = Math.floor(Math.random() * 2);
        console.log("Your health is now: " + userHealth + "!");
}
}
youHit只计算一次。因此,一旦龙对你的玩家造成伤害,它将继续造成伤害

您可以将计算包装在函数中,然后触发它们:

function fightDragon() {
    var userHealth = 5;
    var youHit = function() {
        return Math.floor(Math.random() * 2);
    };
    var damageThisRound = function() {
        return Math.floor(Math.random() * 5 + 1);
    }
    var totalDamage = 0;
    var dragonDamage = function() {
        return Math.floor(Math.random() * 2);
    }

    while (userHealth > 0) {
        var damage = youHit();
        if (damage) {
            console.log("You hit the dragon!");
            totalDamage += damageThisRound();
            console.log("Total damage dealt: " + totalDamage + "!");
            if (totalDamage >= 4) {
                console.log('You slew the dragon!');
                break;
            }
        } else {
            console.log('The dragon has dealt damage to you!');
            userHealth -= dragonDamage();
            console.log('Your health is now: ' + userHealth + '!')
        }
    }

}

在你的龙代码中添加一个你命中的数学回合:

var userHealth = 5;
var youHit = Math.floor(Math.random() * 2);
var damageThisRound = Math.floor(Math.random()*5 + 1);
var totalDamage = 0;
var dragonDamage = Math.floor(Math.random() * 2);
while (userHealth > 0) {
    if (youHit) {
        console.log("You hit the dragon!");
        totalDamage += damageThisRound;
        console.log("Total damage dealt: " + totalDamage + "!");
        if (totalDamage >= 4) {
            console.log("You slew the dragon!");
            userHealth = 0;
        }
        else {
            youHit = Math.floor(Math.random() * 2);
        }
    }
    else {
        console.log("The dragon has dealt damage to you!");
        userHealth -= dragonDamage;
        youHit = Math.floor(Math.random() * 2);
        dragonDamage = Math.floor(Math.random() * 2);
        console.log("Your health is now: " + userHealth + "!");
}
}

在您的例子中,while循环之外的所有变量只计算一次

if (totalDamage >= 4) {
    userHealth = 0;
} else {
    youHit = Math.floor(Math.random() * 2);
}
上面的代码永远不会被执行,因为若龙杀了你们,游戏结束,循环结束

我还添加了这个:如果!!龙的伤害条件,检查龙的伤害是否为零。另一种方法是在伤害计算结果中加1=

var userHealth = 5,
    totalDamage = 0;

while (userHealth > 0) {

    var youHit = Math.floor(Math.random() * 2),
        yourDamage = Math.floor(Math.random()*5 + 1),
        dragonDamage = Math.floor(Math.random() * 2);

    if (youHit) {
        console.log("You hit the dragon for " + yourDamage);
        totalDamage += yourDamage;

        if (totalDamage >= 4) {
            console.log("You slew the dragon!");
            userHealth = 0;
        }

    } else {
        if (!!dragonDamage) {
            console.log("The dragon has dealt damage to you!");
            userHealth -= dragonDamage;
            console.log("Your health is now: " + userHealth + "!");
        }
    }
}
两件事:

1你需要重新计算一个随机值,因为你在“龙对你造成伤害”部分中命中:

youHit = Math.floor(Math.random() * 2);
否则,如果玩家第一次命中率为0,它将始终保持0,并且龙将赢得每次交换

2在“你击中龙”部分,你将玩家的生命值设置为0,以退出while循环,即使玩家的生命值实际上不应为0。如果你想在整个游戏中显示玩家的健康状况,这是一个问题。我建议在while循环中添加一个标志:

var dragonSlain = false;
while (userHealth > 0 && !dragonSlain)
{
    ...
    if (totalDamage >= 4) {
        console.log("You slew the dragon!");
        //userHealth = 0;
        dragonSlain = true;
    }
    ...
}

当巨龙击中玩家时,你永远不会重新计算你的命中率,因此,如果你命中,你将无法再做任何其他事情。非常感谢你的帮助!!我现在明白了!!youHit为false,这意味着在用户点击龙后重新分配youHit的else语句永远不会运行。我应该计算一下,当龙击中用户时,你会再次命中。谢谢你的帮助!!令人惊叹的谢谢你的帮助!!