枚举和案例陈述。即使休息也不会切换

时间:2014-09-27 00:09:03

标签: java enums case

我正在尝试从开始状态开始切换状态。但是当我输入输入以进入失败状态或进入状态时,都不起作用。我曾尝试在之前建议的代码中添加中断,但我担心它没有任何影响。还有其他解决方案吗?

  import java.util.*;

 public class dfa {
  static State state;


 public enum State 
{
    START_STATE,
     GO_STATE,
     FAIL_STATE,
     };

dfa(State state)
 { state = this.state;}

/**
 * @param args
 */
public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    String line;
    int number;
    state = State.START_STATE;   
        switch(state)
            {
            case START_STATE:

                System.out.print("In start state \n ");
                System.out.println("Which state you want to go to? \n");
                number = input.nextInt();
                if(number == 1)
                {
                    state = State.GO_STATE;
                    System.out.print(" Go hello");

                }
                else if(number == 2)
                {
                    state = State.FAIL_STATE;
                System.out.println(" Fail hello ");
                }
                break;

                case FAIL_STATE:  
                System.out.println("fail state");
                break;
                case GO_STATE:
                System.out.println("go state");
               break;

            default:
                System.out.println("Default");
                break;
            }


    System.out.println("made it here");





  }

  }

1 个答案:

答案 0 :(得分:1)

当您更改state的值时,您已经在switch语句的内部,并且如图所示this question,一旦找到匹配,则执行大小写并且控制流到结束时切换声明。

我不完全确定您要完成的任务,但如果您想要更新state并再次输入开关,则必须添加循环。