enum Action{
Left,Right,Up,Down,Restart,Stop
}
public class TestEnum{
public static void main (String[] args){
Choose Action(Action.Restart);
Choose Action(Action.Left);
Choose Action(Action.Right);
Choose Action(Action.Up);
Choose Action(Action.Down);
Choose Action(Action.Stop);
}

public static void ChooseAction(Action action){
Switch(action){
case Left:
System.out.println("你按了左键");
break;

case Right:
System.out.println("你按了右键");
break
;
case Up:
System.out.println("你按了上键");
break;

case Down:
System.out.println("你按了下键");
break;

case Restart:
System.out.println("你按了重启");
break;

case Stop:
System.out.println("你按了停止");
break;
}
}
}

解决方案 »

  1.   

    Choose Action中间为啥要加空格?
      

  2.   


    还有swich语句一直提示用了单个case  不知道什么意思
      

  3.   

    switch不是只能用整型的吗,我记错了吗
      

  4.   


    enum Action {
    Left, Right, Up, Down, Restart, Stop
    }public class TestEnum {
    public static void main(String[] args) {
    ChooseAction(Action.Restart);
    ChooseAction(Action.Left);
    ChooseAction(Action.Right);
    ChooseAction(Action.Up);
    ChooseAction(Action.Down);
    ChooseAction(Action.Stop);
    } public static void ChooseAction(Action action) {
    switch (action) {
    case Left:
    System.out.println("你按了左键");
    break; case Right:
    System.out.println("你按了右键");
    break;
    case Up:
    System.out.println("你按了上键");
    break; case Down:
    System.out.println("你按了下键");
    break; case Restart:
    System.out.println("你按了重启");
    break; case Stop:
    System.out.println("你按了停止");
    break;
    }
    }
    }
      

  5.   


    其实相当于是整形,只不过是枚举里面的索引而已。不是字符串。代码是没问题的
    问题1:Choose Action 里面有空格,空格去掉。
    问题2:Switch(action) s为小写。
    建议楼主用IDE工具开发DEMO,你用记事本敲,很容易敲错的。