java中switch语句的相关问题!急

来源:百度知道 编辑:UC知道 时间:2024/06/17 11:49:41
public class Test {
public static void main(String[] args)
{
String s;
s="*";
//Float f=new Float(s);
switch (s)
{
case “*”:System.out.println("*");break;
case “-”:System.out.println("*");break;
}
}
}
请问上述switch语句中的参数有什么问题呢?

java switch() 只能是:
int
short
byte
char

而你的是string,当然不能.

switch语句中的参数只能是离散的变量或枚举类型。
离散变量为:int short byte char
枚举类型需自己定义,例如:
public enum Symble{
*,-
}
……
switch(Symble)
{
case "*" ……
case "-" ……
}

java switch() 只能是:
int
short
byte
char

而你的是string,当然不能.

靠发现基础知识开始忘记了,开来要时刻巩固啊
switch的参数好像自能是数字

仅仅可以为一下的情况
char
int
short
byte

不能是引用类型的