读书人

switch(),不是()里面的只能是int sh

发布时间: 2011-12-28 22:45:21 作者: rapoo

switch(),不是()里面的只能是int short byte char的么??
public class Test {
public enum Dogs {collie, harrier};
public static void main(String [] args) {
Dogs myDog = Dogs.collie;
switch (myDog) {//怎么看可以呢?
case collie:
System.out.print( "collie ");
case harrier:
System.out.print( "harrier ");
}

}
}
结束输出collie harrier

[解决办法]
Since you are using enum, then you should know it 's features. It can be used in switch structure, together with anything that can be implicitly casted to int.
[解决办法]
后面加个.values()看看,具体的可参考下面的例子

public class EnumDemo{
public enum Seasons {
winter,spring,summer,fall;
}
public static void main(String[] args){
for(Seasons s:Seasons.values()){
System.out.println(s);
}
}

读书人网 >J2SE开发

热点推荐