2011-12-23 92 views
0
<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,那么我们显示Finally Friday。我想如果日!= 5,然后显示'最后的东西'..类似的其他人也...Switch case - else condition

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

+0

你的意思是要更改或添加到文本中'default'情况? – 2011-12-23 12:39:22

+0

@FelixKling:我想如果5不执行,那么应该发生其他的事情... 6或0可能会执行... – 2011-12-23 12:40:09

+2

使用普通老的if ... else有什么问题? – 2011-12-23 12:41:27

回答

0

你可以这样做:

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; 
} 
+0

这些行可能有用,但请注意,如果日期=== 5,您的代码将写入“Finally Friday”,然后是“Not Friday”(其他日期等等)。 – nnnnnn 2011-12-24 03:42:05

0

一个标准的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!"); 
} 
+2

这是行不通的。如果值是6,它将只执行第一个匹配的案例加上任何跌落。它永远不会做第二种情况6. – nnnnnn 2011-12-24 03:04:40

0

它就像开关不跳到匹配的情况下,如果它不匹配,它会跳转到匹配的地方。对你的问题的回答“如果案件5不执行,我可以在那个地方做别的事吗?”是否,因为它从来没有达到这种情况。它跳转到下一个匹配的案例或默认值。

2

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

switch语句将执行第一个匹配的情况下,再继续下去(忽略所有进一步的情况下标签),直到它获取到任何一个break语句或开关块的结束 - 但即使您可以通过省略break语句来“落后”到后续案例,交换机也不会提供任何机制来说“在此案例不匹配/执行时执行某些操作,还会继续尝试查找匹配案例”。

你所描述通常会用一系列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!"); 
    } 
} 

如果你需要的情况下,按照特定的顺序来执行使用数组,而不是一个东西。

0

过于复杂的答案。

简化。

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

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

唯一的解决方案,可以制定没有比较。 USE “DEFAULT” PATTERN

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

与其他人相比,它更简单吗?什么是默认模式? – dakab 2016-02-10 19:23:17