System.out.println("输入数字:");
Scanner sc=new Scanner(System.in);
int i=sc.nextInt();
switch(i)
{
case 1:
System.out.println("hello world!");
case 2:
System.out.println("test2");
default:
System.out.println("not found");
}
在jdk1.7之前,switch支持的多种数据类型 int float double byte 以及对应的封装类型 都可以支持。
其对应的封装类型会自动转换为int ,所以 float double 等数据会出现失精的情况。
那么switch到底能不能支持String类型的数据判断呢?String test="test2";
switch(test)
{
case "test":
System.out.println("hello");
break;
case "test2":
System.out.println("hello world");
break;
}
在jdk1.7开始,测试是否支持String 结果是支持的
运行结果正常,从jdk1.7开始,switch支持更多的数据类型,字符串,字符类型,枚举,布尔都支持!