繁体   English   中英

切换案例 - 其他条件

[英]Switch case - else condition

<script type="text/javascript">
//You will receive a different greeting based
//on what day it is. Note that Sunday=0,
//Monday=1, Tuesday=2, etc.

var d = new Date();
var theDay = d.getDay();
switch (theDay)
{
case 5:
  document.write("Finally Friday");
  break;
case 6:
  document.write("Super Saturday");
  break;
case 0:
  document.write("Sleepy Sunday");
  break;
default:
  document.write("I'm looking forward to this weekend!");
}
</script>

如果theDay = 5 ,那么我们显示theDay = 5 Finally Friday 我想要如果 theDay !=5,然后显示“终于有些东西”.. 对于其他人也是如此......

没有 If/else 条件是否可能。 如果案例 5 没有执行,我可以在那个地方做其他事情吗?

没有 If/else 条件是否可能。 如果案例 5 没有执行,我可以在那个地方做其他事情吗?

不。

switch 语句将执行第一个匹配的 case,然后继续执行(忽略所有进一步的 case 标签)直到它到达 break 语句或 switch 块的末尾 - 但即使你可以通过省略 break 语句,switch 不提供任何机制来说明“当这种情况不匹配/执行时做某事,但也要继续尝试寻找匹配的情况”。

您所描述的内容通常是通过一系列 if/else 语句完成的:

var d = new Date(),
    theDay=d.getDay(),
    matched = false;

if (theDay === 5) {
  matched = true;
  document.write("Finally Friday");
} else {
  // your != 5 case here
}
if (theDay === 6) {
  matched = true;
  document.write("Super Saturday");
} else {
  // your != 6 case here
}
if (theDay === 0) {
  matched = true;
  document.write("Sleepy Sunday");
} else {
  // your != 0 case here
}

// default when none matched:
if (!matched) {
  document.write("I'm looking forward to this weekend!");
}

请注意,我添加了一个matched标志以允许默认值工作。 请注意,没有else if语句,因为您需要执行每个 if/else 对。

如果你真的决定使用 switch 语句,你可以做一些像下面这样的傻事

var d = new Date(),
    theDay = d.getDay(),
    c,
    cases = { // pre-list all the "not" cases
             "!5" : true,
             "!6" : true,
             "!0" : true
            };

// add case for theDay and remove the "not" case for theDay (if there is one)
cases[theDay] = true;
if (cases["!" + theDay])
   delete cases["!" + theDay];

for (c in cases) {
   switch(c) {  
      case "5":
         document.write("Finally Friday");
         break;
      case "!5":
         document.write("Finally Something");
         break;
      case "6":
         document.write("Super Saturday");
         break;
      case "!6":
         document.write("Finally Something - but not 6");
         break;
      case "0":
         document.write("Sleepy Sunday");
         break;
      case "!0":
         document.write("Finally Something - but not 0");
         break;
      default:
         document.write("I'm looking forward to this weekend!");
   }
}

如果您需要以特定顺序执行案例,请使用数组而不是对象。

它就像 switch 会跳转到匹配的大小写,如果不匹配,它将跳转到匹配的内容。 你的问题的答案“如果案例5没有执行,我可以在那个地方做其他事情吗?” 是不,因为它根本没有达到那种情况。 它跳转到下一个匹配的案例或默认值。

没有 If/else 条件是否可能。 如果案例 5 没有执行,我可以在那个地方做其他事情吗?

使用默认代码块,如下所示。 任何与案例不匹配的东西都将回退到默认的。

switch(expression) {
      case x:
        // code block
        break;
      case y:
        // code block
        break;
      default:
        // code block
    }

这是如何工作的:

  • 将表达式的值与每种情况的值进行比较。
  • 如果匹配,则执行关联的代码块。
  • 如果不匹配,则执行默认代码块。

您可以执行以下操作:

var something = 0;

switch (theDay) {
    case 5:
      document.write("Finally Friday");
      something = 15;
      break;
    case 6:
      document.write("Super Saturday");
      something = 16;
      break;
    case 0:
      document.write("Sleepy Sunday");
      something = 20;
      break;
    default:
      document.write("I'm looking forward to this weekend!");
}


switch (something) {
    case 15: document.write("Not Friday");   break;
    case 16: document.write("Not Saturday"); break;
    case 20: document.write("Not Sunday");   break;
    default: document.write("Nothing");      break;
}

标准的if / else将是实现这一目标的最佳方式。 我想知道你为什么要这样做。

也就是说,它不是很优雅,但您可以尝试将以下内容添加到 switch 语句的顶部:

case 0:
case 1:
case 2:
case 3:
case 4:
case 6:
   document.write("Finally Something");

给你:

switch (theDay)
{
case 0:
case 1:
case 2:
case 3:
case 4:
case 6:
   document.write("Finally Something");
case 5:
  document.write("Finally Friday");
  break;
case 6:
  document.write("Super Saturday");
  break;
case 0:
  document.write("Sleepy Sunday");
  break;
default:
  document.write("I'm looking forward to this weekend!");
}

过于复杂的答案。

简化。

var switchElse = true;
switch (CHECK_SOMETHING) 
{
    case "SOME_VALUE":
        ...DO SOMETHING...
        switchElse = false;
        break;
    default:
}

if (switchElse) 
{
        ...DO ELSE...
}

无需比较即可制定的唯一解决方案。 使用“默认”模式

var myValue = "Friday"
switch (CHECK_SOMETHING)
{
    case "SOME_VALUE": 
         myValue = "Some Other Day";
    default:
}

暂无
暂无

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

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