有没有更简单的方法来解决此问题而无需使用太多的if语句?如何?

时间:2019-05-02 21:20:42

标签: if-statement printing count state fsm

我们遇到一个问题,要求使用字母QDN(四分之一,角钱,镍币)创建一个有限状态机,该状态机仅在加40美分时才能接受。我有使用IF语句概念的基础知识。我想知道是否有更简单的方法来花费更少的时间?

我曾尝试制作过很多if案例,但是使用该方法有很多步骤。

public class FSA2_rpf4961 {

public static void main(String[] args) {
    //The program will read in a sequence of strings and test them against a 
    //FSM. Your strings may not contain blank spaces
    System.out.println("Enter string to test or q to terminate");
    Scanner in = new Scanner (System.in);
    String testString = in.next();

    while (!testString.equals("q"))
    {
        String testOutput = applyFSA2(testString);
        System.out.println("For the test string "+testString+
                                ", the FSM output is "+testOutput);
        System.out.println("Enter next string to test or q to terminate:");
        testString = in.next();

    }
    in.close();

}
public static String applyFSA(String s) {
   String currentOut = "0";                // initial output in s0
   String currentState = "s0";             // initial state

   int i = 0;

   while (i<s.length())
   {
            //quarter first
           if (currentState.equals("s0") && s.charAt(i) == 'q')
           {
                    currentState = "s1";  
                    currentOut += 25;  // collect output on move to s1
           }
           else if (currentState.equals("s1") && s.charAt(i)  == 'd') {
            currentState = "s2";
            currentOut += 10;
           }
           else if (currentState.equals("s2") && s.charAt(i)  == 'n') {
            currentState = "s3";
            currentOut += 5;
           }
           else if (currentState.equals("s1") && s.charAt(i)  == 'n') {
            currentState = "s4";
            currentOut += 5;
           }
           else if (currentState.equals("s4") && s.charAt(i)  == 'd') {
            currentState = "s3";
            currentOut += 10;
           }

           //dime first
           else if (currentState.equals("s0") && s.charAt(i) == 'd')
           {
                    currentState = "s5";
                    currentOut += 10;   // d
            }

我们需要它仅在其增加40美分时接受。这让我很难理解。

1 个答案:

答案 0 :(得分:0)

我将在这里进行一般性的讨论,因为您的当前代码很奇怪,几乎可以断定。我不太了解您的“输出”应该是什么。

FSM是一组状态以及输入如何引起状态转换的定义。看来您开始时大致是这样做的,但是currentOut坏了。 如果您的目标是获得一笔总和,那您将失去练习的全部重点。总和是多少都没有关系。重要的是,最后您处于什么状态-尤其是您是否处于“接受”状态,在这种情况下,即字符串等于40美分的状态。

关于如何在没有成千上万个if的情况下实现FSM,通常可以将状态存储在数组中,而将状态名称改为状态号。到那时,理论上您可以摆脱所有{em {em}} 您的if。 (不过,在现实世界中,您可能仍然希望忽略(q | d | n)以外的其他字符。)

请考虑以下内容(伪代码):

    // Each array in `graph` represents a state.
    // Each entry is the state number (ie: index into `graph`) to go to
    // when you're in that state and see the corresponding char.
    //
    // BTW, the state graph makes a lot more sense when you consider nickels first.
    // A nickel takes you to the "next" state, and dimes and quarters act like
    // 2 and 5 nickels, respectively. When you do that, a pattern shows up.
    graph = [
      //  n  d  q
      //-----------
        [ 1, 2, 5 ], // s0
        [ 2, 3, 6 ], // s1
        [ 3, 4, 7 ], // s2
        [ 4, 5, 8 ], // s3
        [ 5, 6, 9 ], // s4
        [ 6, 7, 9 ], // s5
        [ 7, 8, 9 ], // s6
        [ 8, 9, 9 ], // s7
        [ 9, 9, 9 ], // s8
        [ 9, 9, 9 ]  // s9 (fail state)
    ]
    start = 0
    accept = 8
    fail = 9

    // at this point, walking the graph is trivial.
    state = start
    for each char c in s:
        index = "ndq".indexOf(c) // n->0, d->1, q->2, others -> -1
        state = graph[state][index]


    // Once the loop's done:
    //   if state == accept, you have exactly 40c.
    //   if state == fail, you have >40c. A FSM won't tell you how much,
    //      because FSMs can't count.
    // any other state represents a known amount that's less than 40c.