• JS——switch case


    语法:

    switch(n)
    {
    case 1:
      执行代码块 1
      break;
    case 2:
      执行代码块 2
      break;
    default:
      n 与 case 1 和 case 2 不同时执行的代码
    }

    工作原理:首先设置表达式 n(通常是一个变量)。随后表达式的值会与结构中的每个 case 的值做比较。如果存在匹配,则与该 case 关联的代码块会被执行。请使用 break 来阻止代码自动地向下一个 case 运行。

    实例:

    var day=new Date().getDay();
    switch (day)
    {
    case 6:
      x="Today it's Saturday";
      break;
    case 0:
      x="Today it's Sunday";
      break;
    default:
      x="Looking forward to the Weekend";
    }

    结果:

    Looking forward to the Weekend

     特殊写法:

    <script>
        switch ("香蕉") {
            case "香蕉": case "苹果": case "葡萄":
                alert("你喜欢吃水果");
                break;
            default:
                alert("你不喜欢水果吗?");
        }
    </script>
  • 相关阅读:
    UVa 107 The Cat in the Hat
    UVa 591 Box of Bricks
    UVa 253 Cube painting
    UVa 10161 Ant on a Chessboard
    UVa 401 Palindromes
    UVa 465 Overflow
    我不知道
    消防局的设立
    某CF的D
    保安站岗
  • 原文地址:https://www.cnblogs.com/wuqiuxue/p/7685768.html
Copyright © 2020-2023  润新知