Switch语句多次运行

时间:2019-12-30 21:43:44

标签: javascript switch-statement

我又回到了原地,就像以前一样受挫……我的比赛已经走得更远,但是回头看比赛的一部分,我要检查一下你是否有药水,如果有的话,然后尝试使用它,它会计算当前的生命值和该药水的增加量是否大于玩家的最大生命值。如果是,它会告诉您您不能喝,如果不是,则可以让您喝。然后,如果您没有任何药水而尝试使用它,那么它会告诉您您没有任何药水。

我遇到的问题是它运行了两次,我不确定为什么。这是Switch语句的一个片段:

while (currentarea == "Hero's Turn") {
    if (hTurn == true) {
        switch (classType) {
            case "Warrior":
            case "Archer":
                switch (input) {
                    case "punch":
                    case "p":
                        Punch();
                        break;
                    case "kick":
                    case "k":
                        Kick();
                        break;
                    case "attack":
                    case "a":
                        Attack();
                        break;
                    case "health potion":
                    case "h":
                        HealthPotion();
                        break;
                    default:
                }
            case "Mage":
                switch (input) {
                    case "fire":
                    case "f":
                        FireMagic();
                        break;
                    case "wind":
                    case "w":
                        WindMagic();
                        break;
                    case "frost":
                    case "c":
                        IceMagic();
                        break;
                    case "health potion":
                    case "h":
                        HealthPotion();
                        break;
                    case "mana potion":
                    case "m":
                        ManaPotion();
                        break;
                    default:
                }
            default:
        }
        return;
    }
}

我试图将Archer移到自己的生产线上,这使其运行了3次。问题出在战士和弓箭手身上,法师只按计划推送了一次信息。

这是该功能的代码:

function HealthPotion() {
    if (healthPotion.AmountOf < 1) {
        $("<p>Sorry, you don't have any more of those.</p>").hide().insertBefore("#placeholder").fadeIn(1000);
        checkH();
        return;
    }
    if (healthPotion.AmountOf != 0) {
        if(battleHealth + healthPotion.Health <= maxHealth) {
            $("<p>You regained some Health! You regained " + healthPotion.Health + " HP!</p>").hide().insertBefore("#placeholder").fadeIn(1000);
            battleHealth += healthPotion.Health;
            document.getElementById("battleHealth").innerHTML = battleHealth;
            healthPotion.AmountOf--;
            document.getElementById("healthPotion.AmountOf").innerHTML = healthPotion.AmountOf;
            checkE();
            return;
        }
        else {
            $("<p>Your health isn't damaged enough for that! It would be a waste to use that right now.<br />Press enter to carry on.</p>").hide().insertBefore("#placeholder").fadeIn(1000);
            checkH();
            return;
        }
    }
}

这是我构造药水的方式:

var healthPotion = { //Recovers battle HP.
   Name: "Health Potion",
   Health: 20,
   AmountOf: 5,
   Value: 30,
}

这是循环到达的地方(敌人和英雄检查):

function checkE() {
    if (eHealth >= 1) {
        currentarea = "Enemy's Turn";
        document.getElementById("currentarea").innerHTML = currentarea;
        hTurn = false;
        eTurn = true;
        return;
    }
    else if (eHealth <= 0) {
        eHealth = 0;
        document.getElementById("eHealth").innerHTML = eHealth;
        currentarea = "Death";
        win++;
        document.getElementById("win").innerHTML = win;
        hTurn = false;
        eTurn = false;
        return;
    }
}
function checkH() {
    if (battleHealth >= 1) {
        currentarea = "Battle";
        document.getElementById("currentarea").innerHTML = currentarea;
        eTurn = false;
        hTurn = true;
        return;
    }
    else if (battleHealth <= 0) {
        battleHealth = 0;
        document.getElementById("battleHealth").innerHTML = battleHealth;
        currentarea = "Death";
        document.getElementById("currentarea").innerHTML = currentarea;
        lose++;
        document.getElementById("lose").innerHTML = lose;
        return;
    }
}

在这一点上,我感到很沮丧,因为在所有代码中,我都具有switch语句和嵌套函数,但这是我唯一看到此问题的领域。 我的主要浏览器是Firefox,但我在Chrome和Edge上进行了测试,两者的结果相同。 IE不会玩我的游戏,主要是因为我尚未对其进行编码。

在发布到这里之前,我环顾了其他人的问题,没有发现与该特定问题相符的任何内容。

在每个浏览器的管理控制台中,都没有引发任何错误,只是多行重复的语言。

screen shot of the double verbage, playing as a warrior

如果您继续玩耍并承受足以使用该药水的伤害,则会看到以下内容:

screen shot of the double verbage, both using and saying cannot use

1 个答案:

答案 0 :(得分:2)

您突破了内部开关,该中断对外部开关没有任何作用,因此当您拥有“战士”时,它会执行这种情况,并且由于在其级别上没有中断,因此转到“法师”

switch (classType) {
  case "Warrior":
  case "Archer":
    switch (input) {
      case "punch":
      case "p":
        Punch();
        break;
      case "kick":
      case "k":
        Kick();
        break;
      case "attack":
      case "a":
        Attack();
        break;
      case "health potion":
      case "h":
        HealthPotion();
        break;
      default:
    }
    break; // <-- YOU NEED THIS HERE
  case "Mage":
    switch (input) {
      case "fire":
      case "f":
        FireMagic();
        break;
      case "wind":
      case "w":
        WindMagic();
        break;
      case "frost":
      case "c":
        IceMagic();
        break;
      case "health potion":
      case "h":
        HealthPotion();
        break;
      case "mana potion":
      case "m":
        ManaPotion();
        break;
      default:
    }
    break; // <-- YOU NEED THIS HERE IF YOU DO SOMETHING IN DEFAULT
  default:
}