<script type="text/JavaScript"> //首先,我们用score变量来存储分数,假设为65 var score = 65; //用分数除以10,parseInt的作用是把它转换为整数, //暂时不用深究,()内最后的结果为6 switch (parseInt(score / 10)) { //switch开始实现判断过程,case6得到满足 case 0: case 1: case 2: case 3: case 4: case 5: //根据不同的等级做出不同的行为。 //冒号后面的语句就是行为 //case0到5的行为都是下面这个语句 degree = "恭喜你,又挂了!"; break; case 6: degree = "勉强及格"; break; case 7: degree = "凑合,凑合" break; case 8: degree = "8错,8错"; break; case 9: case 10: degree = "高手高手,佩服佩服"; }//end of switch </script>
记得在每个case所执行的语句里添加上一个break语句。为了理解break的作用,我们来看看如果没有break会怎么样: <script type="text/JavaScript"> switch (parseInt(score / 10)) { case 0: case 1: case 2: case 3: case 4: case 5: degree = "恭喜你,又挂了!"; case 6: degree = "勉强及格"; case 7: degree = "凑合,凑合" case 8: degree = "8错,8错"; case 9: case 10: degree = "高手高手,佩服佩服"; }//end of switch </script>在上面的代码中,如果成绩是50分,那么score/10就是5,则case5后面的语句将会得到执行,同样,case6、7等等后面的语句都会得到执行。也就是说,我们会得到:”恭喜你,又挂了!勉强及格凑合,凑合8错,8错高手高手,佩服佩服“这样没有意义的评价。这就是swtitch语句的执行逻辑,当发现某个case满足后,该switch中在该case后的所有语句都会得到执行。第一个例子中的break就是为了让switch”停下来“。
var y=(new Date()).getFullYear(); var m=(new Date()).getMonth(); var d=31; switch(m) { case 1: d=(!y%4 && y%100)||(y%400)?29:28; break; case 3: case 5: case 8: case 10: d=30; break; default: d=31; }
上面有点小问题 d=(!(y%4) && y%100)||(!(y%400))?29:28;
如果 switch (这里是变量名) 下面能否 case “变量名”:就是说 switch 的条件 和 case 能否 不用数字 而使用 变量 ?例如 switch (flag) case adds:case editor:case del ......
switch() { case: break; default: break; }
参考: var d=new Date() 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>
//首先,我们用score变量来存储分数,假设为65 var score = 65;
//用分数除以10,parseInt的作用是把它转换为整数,
//暂时不用深究,()内最后的结果为6 switch (parseInt(score / 10)) {
//switch开始实现判断过程,case6得到满足
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
//根据不同的等级做出不同的行为。
//冒号后面的语句就是行为
//case0到5的行为都是下面这个语句
degree = "恭喜你,又挂了!";
break;
case 6:
degree = "勉强及格";
break;
case 7:
degree = "凑合,凑合"
break;
case 8:
degree = "8错,8错";
break;
case 9:
case 10:
degree = "高手高手,佩服佩服";
}//end of switch
</script>
<script type="text/JavaScript">
switch (parseInt(score / 10)) {
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
degree = "恭喜你,又挂了!";
case 6:
degree = "勉强及格"; case 7:
degree = "凑合,凑合"
case 8:
degree = "8错,8错";
case 9:
case 10:
degree = "高手高手,佩服佩服";
}//end of switch
</script>在上面的代码中,如果成绩是50分,那么score/10就是5,则case5后面的语句将会得到执行,同样,case6、7等等后面的语句都会得到执行。也就是说,我们会得到:”恭喜你,又挂了!勉强及格凑合,凑合8错,8错高手高手,佩服佩服“这样没有意义的评价。这就是swtitch语句的执行逻辑,当发现某个case满足后,该switch中在该case后的所有语句都会得到执行。第一个例子中的break就是为了让switch”停下来“。
var m=(new Date()).getMonth();
var d=31;
switch(m)
{
case 1:
d=(!y%4 && y%100)||(y%400)?29:28;
break;
case 3:
case 5:
case 8:
case 10:
d=30;
break;
default:
d=31;
}
d=(!(y%4) && y%100)||(!(y%400))?29:28;
下面能否
case “变量名”:就是说
switch 的条件 和 case 能否 不用数字 而使用 变量 ?例如
switch (flag)
case adds:case editor:case del
......
{
case:
break;
default:
break;
}
var d=new Date()
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>