Java控制流程复习(2)switch语句
概述
switch 语句相当于 if else的另一种表达方式。
示例1:switch
switch可以使用byte,short,int,char,String,enum。
注: 每个表达式结束,都应该有一个break;
注: String在Java1.7之前是不支持的, Java从1.7开始支持switch用String的,编译后是把String转化为hash值,其实还是整数。
注: enum是枚举类型,在枚举章节有详细讲解。
/**
* FileName: Demo01.java
* @Description: TODO(用一句话描述该文件做什么)
* All rights Reserved, Designed By Gujiakai
* Copyright: Copyright(C) 2021-2022
* Company Nanjing Xiaozhuang University
* @author: Gu Jiakai
* @version V1.0
* Createdate: 2021年7月6日 上午6:10:01
*
* Modification History
* Date Author Version Discription
* -----------------------------------------------------------------------------------
* 2021年7月6日 Jaya 1.0 1.0
* Why & What is modified: <修改原因描述>
*/
package switch_case;
/**
* @ClassName: Demo01.java
* @Description: TODO(用一句话描述该文件做什么)
* @author Gu jiakai
* @version V1.0
* @Date 2021年7月6日 上午6:10:01
*/
public class Demo01 {
public static void main(String[] args) {
int day=5;
switch(day)
{
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;
default:
System.out.println("日期有误!");
break;
}
}
}
//result:
//星期五
示例2:练习-季节
通过Scanner输入月份,然后使用switch 判断季节。
/**
* FileName: Exercise_Season.java
* @Description: TODO(用一句话描述该文件做什么)
* All rights Reserved, Designed By Gujiakai
* Copyright: Copyright(C) 2021-2022
* Company Nanjing Xiaozhuang University
* @author: Gu Jiakai
* @version V1.0
* Createdate: 2021年7月6日 上午6:15:40
*
* Modification History
* Date Author Version Discription
* -----------------------------------------------------------------------------------
* 2021年7月6日 Jaya 1.0 1.0
* Why & What is modified: <修改原因描述>
*/
package switch_case;
import java.util.Scanner;
/**
* @ClassName: Exercise_Season.java
* @Description: TODO(用一句话描述该文件做什么)
* @author Gu jiakai
* @version V1.0
* @Date 2021年7月6日 上午6:15:40
*/
public class Exercise_Season {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int season=sc.nextInt();
switch(season)
{
case 12:
System.out.println("冬天");
break;
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;
default:
System.out.println("输入有误!");
break;
}
}
}
//result:
//9
//秋天
改进代码
/**
* FileName: Exercise_Season.java
* @Description: TODO(用一句话描述该文件做什么)
* All rights Reserved, Designed By Gujiakai
* Copyright: Copyright(C) 2021-2022
* Company Nanjing Xiaozhuang University
* @author: Gu Jiakai
* @version V1.0
* Createdate: 2021年7月6日 上午6:15:40
*
* Modification History
* Date Author Version Discription
* -----------------------------------------------------------------------------------
* 2021年7月6日 Jaya 1.0 1.0
* Why & What is modified: <修改原因描述>
*/
package switch_case;
import java.util.Scanner;
/**
* @ClassName: Exercise_Season.java
* @Description: TODO(用一句话描述该文件做什么)
* @author Gu jiakai
* @version V1.0
* @Date 2021年7月6日 上午6:15:40
*/
public class Exercise_Season {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int season=sc.nextInt();
switch(season)
{
case 12:
case 1:
case 2:
System.out.println("冬天");
break;
case 3:
case 4:
case 5:
System.out.println("春天");
break;
case 6:
case 7:
case 8:
System.out.println("夏天");
break;
case 9:
case 10:
case 11:
System.out.println("秋天");
break;
default:
System.out.println("输入有误!");
break;
}
}
}
//result:
//9
//秋天