当前位置: 代码迷 >> J2SE >> switch(),不是()里面的只能是int short byte char的么?解决思路
  详细解决方案

switch(),不是()里面的只能是int short byte char的么?解决思路

热度:572   发布时间:2016-04-24 15:24:37.0
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);
}
}
  相关解决方案