简体   繁体   English

简单的JavaScript if / else语句

[英]Simple JavaScript if / else statements

I'm learning Javascript. 我正在学习Javascript。 Here is my code. 这是我的代码。 It is a simple program in which the user fights a dragon, but I added an extra bit where if the dragon reduces the user's health to 0, the code finishes. 这是一个简单的程序,其中用户与龙搏斗,但是我添加了一点,如果龙将用户的健康降低到0,则代码完成。 Whenever I run this code, though, once the dragon begins to reduce the health of the user, that is all that happens. 但是,无论何时我运行此代码,一旦龙开始降低用户的健康状况,这一切就发生了。 The user isn't able to trade blows with the dragon. 用户无法与龙进行打击。 What am I doing wrong? 我究竟做错了什么?

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 is only calculated once. youHit仅计算一次。 Because of that, once the dragon deals damage to your player, it will keep dealing damage. 因此,一旦龙对玩家造成伤害,它将继续造成伤害。

You can wrap the calculations in a function and fire them instead: 您可以将计算包装在一个函数中,然后触发它们:

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 + '!')
        }
    }

}

Add a you hit math round to your dragon code: 在您的龙代码中添加一个您命中的数学运算:

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 + "!");
}
}

In your case, all the variables outside the while loop are calculated only once. 在您的情况下,while循环之外的所有变量仅计算一次。

if (totalDamage >= 4) {
    userHealth = 0;
} else {
    youHit = Math.floor(Math.random() * 2);
}

This code above won't ever be executed, since if dragon kill you, game is over and loop is ended! 上面的代码将永远不会执行,因为如果龙杀死您,游戏结束并且循环结束!

Also I've added this: if (!!dragonDamage) condition to check if generated damage for a dragon is not zero. 另外,我还添加了以下内容:if(!! dragonDamage)条件来检查对龙产生的伤害是否不为零。 Another way is to add 1 to the result of dragonDamage calculations =) 另一种方法是在DragonDamage计算的结果中加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 + "!");
        }
    }
}

Couple things: 几件事情:

1) You'll want to recalculate a random value for youHit in the "dragon has dealt damage to you" section: 1)您需要为您重新计算一个随机值。“龙对您造成了伤害”部分中的命中:

youHit = Math.floor(Math.random() * 2);

Otherwise, if the player's hit is 0 the first time, it will always stay 0 and the dragon will win every exchange. 否则,如果玩家的第一次击中为0,它将始终保持为0,龙将赢得每次交换。

2) In the "You hit the dragon" section, you are setting the player's health to 0 in order to exit the while loop, even though the player's health should not actually be 0. This is a problem if you ever intend to display the player's health throughout the game. 2)在“您击中了龙”部分中,您将播放器的运行状况设置为0,以退出while循环,即使播放器的运行状况实际上不应该为0。如果您打算显示整个游戏过程中玩家的健康状况。 I suggest adding a flag to the while loop: 我建议在while循环中添加一个标志:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM