我的 switch 语句格式是否错误?

发布于 2024-10-31 13:37:28 字数 3504 浏览 0 评论 0原文

我一直在做一个“剪刀石头布”游戏,看看我是否了解 JavaScript 的基础知识。一切都运行良好,直到我到达 fight() 中第 30 行到第 71 行的 if 语句:我已经调整了应答变量以指示它如何“if'ing”。它似乎返回整个“if”中每个子“if”的默认值。我是否在次要条件中使用了错误的变量?

radioGroup.html

    <script type = "text/javascript">
      //<![CDATA[
      // from radioGroup.html
      function fight(){
        var randomChoice = Math.random();
        var threeChoice = Math.floor (randomChoice * 3);
        var weapon = document.getElementsByName("weapon");

        for (i = 0; i < weapon.length; i++){
          currentWeapon = weapon[i];

          if (currentWeapon.checked){
            var selectedWeapon = currentWeapon.value;
          } // end if

        } // end for

        var cpuChoice = new Array("Paper", "Rock", "Scissors")
        var reply = "xxx"
        if (threeChoice === 0) {
            if (weapon === "Paper") {
                reply = "11";
                }
            else if (weapon === "Rock") {
                    reply = "12";
                }
            else if (weapon === "Scissors") {
                    reply = "13";
                    }
            else {
                    reply = "what1";
                    }
        }else if (threeChoice === 1) {
                if (weapon === "Paper") {
                reply = "21";
                }
            else if (weapon === "Rock") {
                    reply = "22";
                }
            else if (weapon === "Scissors") {
                    reply = "23";
                    }
            else {
                    reply = "what2";
                    }
        }else if (threeChoice === 2) {
                if (weapon === "Paper") {
                reply = "31";
                }
            else if (weapon === "Rock") {
                    reply = "32";
                }
            else if (weapon === "Scissors") {
                    reply = "33";
                    }
            else {
                    reply = "what3";
                    }
        }else {
            reply = "hay now?";
            }
        var output = document.getElementById("output");
        var response = "<h2>You have chosen ";
        response += selectedWeapon + "<h2>The CPU has chosen ";
        "<\/h2> \n";
        response += cpuChoice[threeChoice] + "<\/h2> \n";
        response += reply + "<\/h2> \n";
        output.innerHTML = response;
     } // end function

      //]]>
    </script>
  </head>

  <body>
    <h1>Choose!</h1>
    <form action = "">
      <fieldset>
        <input type = "radio"
               name = "weapon"
               id = "radPaper"
               value = "Paper"
               checked = "checked" />
        <label for = "radPaper">Paper</label>

        <input type = "radio"
               name = "weapon"
               id = "radRock"
               value = "Rock" />
        <label for = "radRock">Rock</label>

        <input type = "radio"
               name = "weapon"
               id = "radScissors"
               value = "Scissors" />
        <label for = "radScissors">Scissors</label>
        <button type = "button"
                onclick = "fight()">
          fight the dragon
        </button>
      </fieldset>
    </form>
    <div id = "output">

    </div>
  </body>
</html>

I have been working on a "Rock Paper Scissors" game to see if I understand the basics of JavaScript. It all works well until I get to the if statement from lines 30 to 71 in the fight(): I have adjusted the reply variable to indicate how it may be "if'ing" through. It seems to be returning the default value of every sub "if" in the overall "if". Am I using the wrong variable for the secondary condition maybe?

radioGroup.html

    <script type = "text/javascript">
      //<![CDATA[
      // from radioGroup.html
      function fight(){
        var randomChoice = Math.random();
        var threeChoice = Math.floor (randomChoice * 3);
        var weapon = document.getElementsByName("weapon");

        for (i = 0; i < weapon.length; i++){
          currentWeapon = weapon[i];

          if (currentWeapon.checked){
            var selectedWeapon = currentWeapon.value;
          } // end if

        } // end for

        var cpuChoice = new Array("Paper", "Rock", "Scissors")
        var reply = "xxx"
        if (threeChoice === 0) {
            if (weapon === "Paper") {
                reply = "11";
                }
            else if (weapon === "Rock") {
                    reply = "12";
                }
            else if (weapon === "Scissors") {
                    reply = "13";
                    }
            else {
                    reply = "what1";
                    }
        }else if (threeChoice === 1) {
                if (weapon === "Paper") {
                reply = "21";
                }
            else if (weapon === "Rock") {
                    reply = "22";
                }
            else if (weapon === "Scissors") {
                    reply = "23";
                    }
            else {
                    reply = "what2";
                    }
        }else if (threeChoice === 2) {
                if (weapon === "Paper") {
                reply = "31";
                }
            else if (weapon === "Rock") {
                    reply = "32";
                }
            else if (weapon === "Scissors") {
                    reply = "33";
                    }
            else {
                    reply = "what3";
                    }
        }else {
            reply = "hay now?";
            }
        var output = document.getElementById("output");
        var response = "<h2>You have chosen ";
        response += selectedWeapon + "<h2>The CPU has chosen ";
        "<\/h2> \n";
        response += cpuChoice[threeChoice] + "<\/h2> \n";
        response += reply + "<\/h2> \n";
        output.innerHTML = response;
     } // end function

      //]]>
    </script>
  </head>

  <body>
    <h1>Choose!</h1>
    <form action = "">
      <fieldset>
        <input type = "radio"
               name = "weapon"
               id = "radPaper"
               value = "Paper"
               checked = "checked" />
        <label for = "radPaper">Paper</label>

        <input type = "radio"
               name = "weapon"
               id = "radRock"
               value = "Rock" />
        <label for = "radRock">Rock</label>

        <input type = "radio"
               name = "weapon"
               id = "radScissors"
               value = "Scissors" />
        <label for = "radScissors">Scissors</label>
        <button type = "button"
                onclick = "fight()">
          fight the dragon
        </button>
      </fieldset>
    </form>
    <div id = "output">

    </div>
  </body>
</html>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

就是爱搞怪 2024-11-07 13:37:28

您关于错误使用开关条件的说法是正确的,“case”是开关中表达式的计算结果。

你应该使用 if 语句,但你可以使用这样的 switch 语句

switch (weapon === "paper") {
case true:
   //do something
   break;
case false:
   // do something else
default:
   // oh no! it wasn't true of false
}

You are correct about using the switch condition incorrectly, the "case" is what the expression in your switch evaluates to.

you should be using if statements, but you can use a switch statement like this

switch (weapon === "paper") {
case true:
   //do something
   break;
case false:
   // do something else
default:
   // oh no! it wasn't true of false
}
安静被遗忘 2024-11-07 13:37:28

case 用于测试简单值,通常是数字。对于你的情况,我建议为石头、剪刀、布分配数字,这样这些数字的顺序就可以清楚地表明谁赢了。例如,石头=0,布=1,剪刀=2。然后,(3 + (a - b)) % 3 会告诉您谁赢了:0 表示平局,1 表示 a 获胜,2 表示 b 赢了。然后,您可以对该操作的结果执行简单的三例切换

case is for testing simple values, usually numeric. For your case, I'd suggest assigning numbers to rock, paper, and scissors, such that the ordering of those numbers makes it clear who won. For example, rock=0, paper=1, scissors=2. Then, (3 + (a - b)) % 3 will tell you who won: 0 is a tie, 1 indicates that a won, and 2 indicates that b won. Then you can do a simple three-case switch over the result of that operation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文