switch (String) 的使用
来源:互联网 发布:锻炼腹肌的软件 编辑:程序博客网 时间:2024/05/22 03:29
switch能够使类似if-else的判断变得简明,但java中switch()不支持string,(jdk1.7支持)。
使用枚举可以弥补这一缺憾。
使用枚举可以弥补这一缺憾。
1 public class SwitchTest{
2 public enum Animal{
3 dog,cat,mouse;
4 public static Animal getAnimalType(String animal){
5 return valueOf(animal.toLowerCase());
6 }
7 }
8
9 public static void main(String[] args){
10 selectAnimal("cat");
11 }
12
13 public static void selectAnimal(String animal){
14 switch(Animal.getAnimalType(animal)){
15 case cat:
16 System.out.println("this is a cat");
17 break;
18
19 case dog:
20 System.out.println("this is a dog");
21 break;
22
23 case mouse:
24 System.out.println("this is a mouse");
25 break;
26 }
27 }
28 }
2 public enum Animal{
3 dog,cat,mouse;
4 public static Animal getAnimalType(String animal){
5 return valueOf(animal.toLowerCase());
6 }
7 }
8
9 public static void main(String[] args){
10 selectAnimal("cat");
11 }
12
13 public static void selectAnimal(String animal){
14 switch(Animal.getAnimalType(animal)){
15 case cat:
16 System.out.println("this is a cat");
17 break;
18
19 case dog:
20 System.out.println("this is a dog");
21 break;
22
23 case mouse:
24 System.out.println("this is a mouse");
25 break;
26 }
27 }
28 }
- switch (String) 的使用
- java switch的使用+switch用String作为条件
- c++ switch/case 使用 string的方法
- switch 的String参数
- 关于switch语句中使用String类型的实现原理
- switch(String)的实现方式
- switch 对 String 的支持
- JDK7支持switch中使用String
- Switch case 参数如何能使用string
- Java switch 语句使用 String 参数
- 黑马程序员 switch 语句使用 String 参数
- JDK7支持switch中使用String
- java switch case 中使用String
- switch string
- 使用switch的注意
- switch函数的使用
- switch语句的使用
- svn switch的使用
- Struts 生命周期
- VSS+SOS 安全的远程配置管理方案
- XP,WIN7双系统安装参照
- 简单测控系统的设计与实现
- lua中load的使用方法
- switch (String) 的使用
- 无法定位程序输入点glPopAttrib于动态连结库OPENGL.dll上
- Makefile.am详解
- 为什么GSM下行的频率要比上行的频率高呢
- 图的邻接表存储,以及广度深度遍历
- 二维数组作为函数参数传递剖析(C语言)
- Unity3D 插件大全
- Android Studio 快捷键
- 运行apache thrift的错误解决