Presentation is loading. Please wait.

Presentation is loading. Please wait.

10.2.switch语句.

Similar presentations


Presentation on theme: "10.2.switch语句."— Presentation transcript:

1 10.2.switch语句

2 何定华 QQ 学习群:

3 switch语句 1.switch语句的语法 2.判断某年月有多少天 3.判断分数对应的等次 4.判断某年月日是这年的第几天 5.总结

4 1.switch语句 switch语句的格式: switch(表达式){ [case 选项1:] [语句1;[break;]]
[default:语句n+1;[break;]] } 说明: 1.把表达式的值与多个不同选项的值进行比较,来决定执行哪些代码。如果表达式的值与选项k相等,则执行其后的代码直到遇到break为止; 2.如果表达式的值与任何选项的值都不相同,则执行default之后的代码; 3.选项可以相同,虽然不建议这样做; 案例:eg10-7.php

5 1.switch语句 案例:eg10-7.php <?php $a=3; switch ($a){
case 1:echo "1111";break; case 2;echo “2222”;break; //可以用分号 case 3: // 向下执行到break或者switch结束为止 case 4;echo " "; case 3:echo “555”; //选项可以相同 default:echo "666"; } ?>

6 switch语句 1.switch语句的语法 2.判断某年月有多少天 3.判断分数对应的等次 4.判断某年月日是这年的第几天 5.总结

7 2.判断某年月有多少天 1)每年的1、3、5、7、8、10和12月都有31天; 2)每年的4、6、9、11月都有30天;
3)闰年的2月是29天,平年的2月是28天; 闰年的条件是: a) 年号能被400整除; b) 年号能被4整除,但是不能被100整除。 根据分析代码如下(案例:eg10-8.php): case 6: case 9: case 11:$days=30;break; case 2:if (($year % 400==0)||($year % 4==0) && ($year % 100)<>0) $days=29; else $days=28; break; default:$days=-1;break; } echo "{$year}年{$month}月有{$days}天"; ?> <?php $year=2008; $month=2; switch($month){ case 1: case 3: case 5: case 7: case 8: case 10: case 12:$days=31;break; case 4:

8 switch语句 1.switch语句的语法 2.判断某年月有多少天 3.判断分数对应的等次 4.判断某年月日是这年的第几天 5.总结

9 3.判断分数对应的等次 若分数大于或者等于90是优秀,分数在60到89之间是及格,分数小于60是不及格,试编程根据分数给出分数对应的等次。
案例:eg10-9.php <?php $score=59.99; if ($score>100) $grade="分数有误!"; else{ $a=(int)($score / 10); switch($a){ case 1:case 2:case 3:case 4:case 5:$grade="不及格";break; case 6:case 7:case 8:$grade="及格";break; case 9:case 10:$grade="优秀";break; default:$grade="分数有误!"; } echo $grade; ?>

10 switch语句 1.switch语句的语法 2.判断某年月有多少天 3.判断分数对应的等次 4.判断某年月日是这年的第几天 5.总结

11 4.判断某年月日是这年的第几天 考虑在文本域中输入年月日,用逗号隔开;
若$year年$month月$day日是这年的第$days日。则$days应该包括 a) $day b) 从第一月到第$month-1月的月份的天数之和; 可以用累加的方法,把1-$month-1月的天数加起来。根据分析,代码如下(案例:eg10-10.php): <html> <body> <form id="form1" name="form1" method="post" action=""> <input type="text" name="textfield" id="textfield" /> <input type="submit" name="button" id="button" value="提交" /> </form> </body> </html> <?php if (isset($_POST['button'])){ list($year,$month,$day)=explode(",",$_POST['textfield']); $days=$day; switch($month-1){ case 11:$days+=30; case 10:$days+=31; case 9:$days+=30; case 8:$days+=31; case 7:$days+=31; case 6:$days+=30; case 5:$days+=31; case 4:$days+=30; case 3:$days+=31; case 2:$days+=28; if (($year % 400==0)||($year % 4==0) && ($year % 100<>0)) $days+=1; case 1:$days+=31; } echo $days; ?>

12 switch语句 1.switch语句的语法 2.判断某年月有多少天 3.判断分数对应的等次 4.判断某年月日是这年的第几天 5.总结

13


Download ppt "10.2.switch语句."

Similar presentations


Ads by Google