<bdo id='Cipwq'></bdo><ul id='Cipwq'></ul>

      <small id='Cipwq'></small><noframes id='Cipwq'>

      <i id='Cipwq'><tr id='Cipwq'><dt id='Cipwq'><q id='Cipwq'><span id='Cipwq'><b id='Cipwq'><form id='Cipwq'><ins id='Cipwq'></ins><ul id='Cipwq'></ul><sub id='Cipwq'></sub></form><legend id='Cipwq'></legend><bdo id='Cipwq'><pre id='Cipwq'><center id='Cipwq'></center></pre></bdo></b><th id='Cipwq'></th></span></q></dt></tr></i><div id='Cipwq'><tfoot id='Cipwq'></tfoot><dl id='Cipwq'><fieldset id='Cipwq'></fieldset></dl></div>
      1. <tfoot id='Cipwq'></tfoot>

      2. <legend id='Cipwq'><style id='Cipwq'><dir id='Cipwq'><q id='Cipwq'></q></dir></style></legend>

        switch case 语句中的表达式

        时间:2023-09-07
        <legend id='GlCNQ'><style id='GlCNQ'><dir id='GlCNQ'><q id='GlCNQ'></q></dir></style></legend>
        <i id='GlCNQ'><tr id='GlCNQ'><dt id='GlCNQ'><q id='GlCNQ'><span id='GlCNQ'><b id='GlCNQ'><form id='GlCNQ'><ins id='GlCNQ'></ins><ul id='GlCNQ'></ul><sub id='GlCNQ'></sub></form><legend id='GlCNQ'></legend><bdo id='GlCNQ'><pre id='GlCNQ'><center id='GlCNQ'></center></pre></bdo></b><th id='GlCNQ'></th></span></q></dt></tr></i><div id='GlCNQ'><tfoot id='GlCNQ'></tfoot><dl id='GlCNQ'><fieldset id='GlCNQ'></fieldset></dl></div>

        <tfoot id='GlCNQ'></tfoot>

            • <bdo id='GlCNQ'></bdo><ul id='GlCNQ'></ul>

              <small id='GlCNQ'></small><noframes id='GlCNQ'>

                <tbody id='GlCNQ'></tbody>
                • 本文介绍了switch case 语句中的表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试创建一个 switch 语句,但我似乎无法使用被评估的表达式(而不是设置的字符串/整数).我可以使用 if 语句轻松地做到这一点,但希望 case 更快.

                  I'm trying to create a switch statement but I can't seem to be able to use an expression that gets evaluated (rather than a set string/integer). I can easily do this with if statements but case should hopefully be faster.

                  我正在尝试以下方法

                  function reward(amount) {
                      var $reward = $("#reward");
                      switch (amount) {
                          case (amount >= 7500 && amount < 10000):
                              $reward.text("Play Station 3");
                              break;
                          case (amount >= 10000 && amount < 15000):
                              $reward.text("XBOX 360");
                              break;
                          case (amount >= 15000):
                              $reward.text("iMac");
                              break;
                          default:
                              $reward.text("No reward");
                              break;
                      }
                  }
                  

                  我是否遗漏了一些明显的东西,或者这不可能?Google 在这种情况下并不友好.

                  Am i missing something obvious or is this not possible? Google hasn't been friendly in this case.

                  任何帮助/指针表示赞赏

                  M

                  推荐答案

                  你总是可以做到的

                  switch (true) {
                    case (amount >= 7500 && amount < 10000):
                      //code
                      break;
                    case (amount >= 10000 && amount < 15000):
                      //code
                      break;
                  
                    //etc...
                  

                  之所以有效,是因为 true 是一个常量,所以第一个 case 语句下的表达式为 true 的代码将被执行.

                  It works because true is a constant, so the code under the first case statement with an expression that evaluates to true will be executed.

                  我想这有点棘手",但我认为使用它没有任何问题.一个简单的 if/else 语句可能会更简洁,您不必担心意外掉线.但无论如何,它就在那里.

                  It's kinda "tricky" I guess, but I see nothing wrong with using it. A simple if/else statement would probably be more concise, and you'd not have to worry about accidental fall-through. But there it is anyway.

                  这篇关于switch case 语句中的表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:大于/小于的 switch 语句 下一篇:在 JavaScript 中打开整数范围

                  相关文章

                    <tfoot id='I9O8U'></tfoot><legend id='I9O8U'><style id='I9O8U'><dir id='I9O8U'><q id='I9O8U'></q></dir></style></legend>
                    1. <i id='I9O8U'><tr id='I9O8U'><dt id='I9O8U'><q id='I9O8U'><span id='I9O8U'><b id='I9O8U'><form id='I9O8U'><ins id='I9O8U'></ins><ul id='I9O8U'></ul><sub id='I9O8U'></sub></form><legend id='I9O8U'></legend><bdo id='I9O8U'><pre id='I9O8U'><center id='I9O8U'></center></pre></bdo></b><th id='I9O8U'></th></span></q></dt></tr></i><div id='I9O8U'><tfoot id='I9O8U'></tfoot><dl id='I9O8U'><fieldset id='I9O8U'></fieldset></dl></div>

                        <bdo id='I9O8U'></bdo><ul id='I9O8U'></ul>

                      <small id='I9O8U'></small><noframes id='I9O8U'>