简单的有限状态机

package com.smart.fsm.phonebank;

public class FSM {

    static State[][] transTable=new State[State.values().length][10];
    
    static{
        transTable[State.start.ordinal()][1]=State.chinese;
        transTable[State.start.ordinal()][2]=State.english;
        transTable[State.chinese.ordinal()][0]=State.end;
        transTable[State.english.ordinal()][0]=State.end;
    }
    
    State current=State.start;
    
    State step(State s,char c){
        current=transTable[s.ordinal()][c-'0'];
        return current;
    }
    
    public static void main(String[] args) {
        FSM fsm=new FSM();
        System.out.println(fsm.step(fsm.current,'1'));
        System.out.println(fsm.step(fsm.current,'0'));
    }

}
package com.smart.fsm.phonebank;

public enum State {
    start,
    chinese,
    english,
    end
}

posted on 2012-10-14 19:56  雨渐渐  阅读(183)  评论(0编辑  收藏  举报

导航