繁体   English   中英

简单的JavaScript if / else语句

[英]Simple JavaScript if / else statements

我正在学习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);
}

上面的代码将永远不会执行,因为如果龙杀死您,游戏结束并且循环结束!

另外,我还添加了以下内容:if(!! dragonDamage)条件来检查对龙产生的伤害是否不为零。 另一种方法是在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 + "!");
        }
    }
}

几件事情:

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;
    }
    ...
}

暂无
暂无

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

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