繁体   English   中英

我在哪里可以使用 JS Switch case 语句来转换这些多个 If/Else 语句?

[英]Where can I use the JS Switch case statement to convert these multiple If/Else statements?

您好,提前感谢您的任何帮助或见解。

我有一个正在开发的 Space Battle 游戏,我想知道是否有办法使用 JavaScript SWITCH 案例语句来替换我在代码中的许多 If/Else 语句。
我不知道switch (expression)从哪一行开始。
switch (expression)是否适合该程序?

// Battle Function = Set up a function that "Holds" the battle

let shipsBattle = (ship1, ship2) => {

  let ships = [ship1, ship2]; // put the ships into an array
  let attack = false;
  let attacking = 0;
  let beingAttacked = 1;
  let temp;
  console.log("%c Attack Begins =================", "font-size: 30px");
  while (ships[beingAttacked].hull > 0) { //While the hull is greater than 0...Keep attacking


    // Attacking Sequence

    if (ships[beingAttacked].hull > 0) {

      console.log("\n"); // Console log the attack information
      console.log(
        `%c ${ships[attacking].name} attacked ${ships[beingAttacked].name}`,
        "color: purple; border: 1px solid grey; font-size: 18px;"
      );

      attack = ships[attacking].attack(); // Generate the attack on the enemy ship
      if (attack === true) {
        ships[beingAttacked].hull -= ships[attacking].firePower; //Increase Fire power
        console.log(
          `%c Attack Successful! ${ships[beingAttacked].name} Hull: ${ships[beingAttacked].hull}`,
          "color: green; font-weight: bold; font-size: 16px;"
        );
      } else {
        console.log(
          `%c Attack Unsuccessful! ${ships[beingAttacked].name} Hull: ${ships[beingAttacked].hull}`,
          "color: red; font-size: 16px;"
        );
      }

      if (ships[beingAttacked].hull <= 0) { // Check if the ship being attacked is still alive
        console.log(
          `%c ${ships[beingAttacked].name} has been destroyed`,
          "color: red; border: 1px solid grey; font-size: 16px;"
        );
        if (ships[beingAttacked] === ussSchwartz) {

          alert("Game Over!!!"); //If the USS Ship is being attacked alert player Game is Over
        } else if (
          ships[beingAttacked].name === alienShips[alienShips.length - 1].name
        ) {
          alert(
            `%c ${ships[beingAttacked].name} destroyed!\nAlien fleet has been destroyed!\nyou have been victorious`,
            "color: green;"
          );
        } //If USS destroys alien fleet, then alert player of victory
        else {
          game.userResponse = prompt(
            `${alienShips[game.targetShip].name} destroyed!!\n${
              ussSchwartz.name
            } Hull: ${
              ussSchwartz.hull
            }\nWould you like to ATTACK the next ship or RETREAT from battle?`,
            ""
          );
          game.targetShip += 1; //PROMPT PLAYER IF THEY WANT TO CONTINUE OR RETREAT
          checkUserPrompt();
          return;
        }
      } else {

        temp = attacking; // Change the attacking/attacked ships
        attacking = beingAttacked;
        beingAttacked = temp;
      }
    }
  }
};

在我看来,您正在检查ships[beingAttacked].hull > 0 ,然后进行一些真假计算。 之后,您将更新ships[beingAttacked]并在ships[beingAttacked].hull < 0上进行比较

因为这样的开关看起来不合适,因为它通常在我们定义了一些东西并且我们没有在每个 else if 中进行不同的比较时使用。

我想,您要做的是进行代码清理。 我建议你:

  • 创建用例的流程图并尝试识别通用代码。 或者可以重用的逻辑。
  • 将该代码移动到单独的函数中,这将增加代码的可读性和再利用。

暂无
暂无

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

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