switch 语句
2016-03-29 16:02
609 查看
说明:
A switch statement executes statements based on the value of a variable or an expression.The if statement makes selections based on a single true or false condition.Assume there are several cases for the question we're facing.To fully account for all the cases, nested if statements were used. But overuse of nested if statements makes a program
difficult to read.To simplify coding for multiple conditions,switch statement is provided.
语法:
switch (switch-expression) { case value1: statement(s)1; break; case value2: statement(s)2; break; ... case valueN: statement(s)N; break; default: statement(s)-for-default; }
注意事项:
①The switch-expression must yield a value of char, byte, short, int, or String type and must always be enclosed in parentheses;
②The value1, . . ., and valueN must have the same data type as the value of the switch-expression.Note that value1, . . ., and valueN are constant expressions, meaning that they cannot contain variables, such as 1 + x;
③When the value in a case statement matches the value of the switch-expression,the statements starting from this case are executed until either a break statement or the end of the switch statement is reached;
④The default case, which is optional, can be used to perform actions when none of the specified cases matches the switch-expression;
⑤The keyword break is optional. The break statement immediately ends the switch statement.
举例:
① 十二生肖是我国重要的民俗文化,其每12年循环一次,并以12种动物(鼠、牛、虎、兔、龙、蛇、马、羊、猴、鸡、狗、猪)标定其中的每一年。现已知2012年为龙年,试根据某人出生年份给出其属相。package Intro_To_Java.Chapter3; import java.util.Scanner; /** * @see 属相判定 * @author JustBeGeek */ public class ChineseZodiac { public static void main(String[] args){ Scanner input = new Scanner(System.in); System.out.println("请输入年份:"); int year=input.next 4000 Int(); int interval=(year-2012)%12; if(interval<0){ interval+=12; } switch(interval+1){ case 1:System.out.println("龙");break; case 2:System.out.println("蛇");break; case 3:System.out.println("马");break; case 4:System.out.println("羊");break; case 5:System.out.println("猴");break; case 6:System.out.println("鸡");break; case 7:System.out.println("狗");break; case 8:System.out.println("猪");break; case 9:System.out.println("鼠");break; case 10:System.out.println("牛");break; case 11:System.out.println("虎");break; case 12:System.out.println("兔");break; } } }
② 星座是西方用于占卜的重要方式,试根据某人出生日期给出其星座。
思路:
Step 1 :依据上图绘制判定表如下:
Step 2 :代码实现
import java.util.Scanner; /** * @see 星座判定 * @author JustBeGeek */ public class Horoscope { public static void main(String[] args){ Scanner input=new Scanner(System.in); System.out.println("请输入月份:"); int month=input.nextInt(); System.out.println("请输入天数:"); int day=input.nextInt(); String horoscope=""; switch(month){ case 1:if(day>=20) horoscope+="水瓶座"; else horoscope+="摩羯座"; break; case 2:if(day>=19) horoscope+="双鱼座"; else horoscope+="水瓶座"; break; case 3:if(day>=21) horoscope+="白羊座"; else horoscope+="双鱼座"; break; case 4:if(day>=20) horoscope+="金牛座"; else horoscope+="白羊座"; break; case 5:if(day>=21) horoscope+="双子座"; else horoscope+="金牛座"; break; case 6:if(day>=22) horoscope+="巨蟹座"; else horoscope+="双子座"; break; case 7:if(day>=23) horoscope+="狮子座"; else horoscope+="巨蟹座"; break; case 8:if(day>=23) horoscope+="处女座"; else horoscope+="狮子座"; break; case 9:if(day>=23) horoscope+="天秤座"; else horoscope+="处女座"; break; case 10:if(day>=24) horoscope+="天蝎座"; else horoscope+="天秤座"; break; case 11:if(day>=23) horoscope+="射手座"; else horoscope+="天蝎座"; break; case 12:if(day>=22) horoscope+="摩羯座"; else horoscope+="射手座"; } System.out.println("您的星座是"+horoscope); } }
相关文章推荐
- Jfinal中的render
- java函数参数传参
- UVA 11552 Fewest Flops 【区间dp】
- 支付宝通知页面notify_url、返回页面return_url
- iOS post一个json字符串到服务器 评论
- linux学习基础之1.0---shell的常见指令
- Eclipse下用Gradle 打包Android 程序
- sqlserver查询指定树形结构的所有子节点
- 未能写入输出文件“c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root
- 终极瘦身指南
- The minimum depth The maximum path sum path sum II
- 精华阅读第 10 期 |解开阿尔法狗(AlphaGo)人工智能的画皮
- 利用 Composer 一步一步构建自己的 PHP 框架(一)——基础准备
- VMware VirtualCenter Server Service服务启动失败
- 计算两个时间戳间隔时间, (可以用于倒计时
- (七)RxJava定时器:timer
- Android studio 制作aar 使用Gradle发布项目到JCenter仓库
- Oracle学习(四)----分组函数
- Struts2+jQuery+JSON 实现输入关键字后提示框显示数据
- Hive学习之六 《Hive进阶— —hive jdbc》 详解