表示DFA Java的Switch语句错误

表示DFA Java的Switch语句错误,java,char,switch-statement,java.util.scanner,dfa,Java,Char,Switch Statement,Java.util.scanner,Dfa,我已经编写了一个Java程序,用switch语句表示DFA,但它不会接受它应该接受的单词。 我尝试添加一个单独的案例,将最终状态发送到输出“word accepted”或“word not accepted”,但这不起作用。 接受的示例词应为: 谷歌 扭动 XXGoooogleXeg 到目前为止,我的代码是: public static void main(String[] args) { System.out.println("Enter a word to run on the D

我已经编写了一个Java程序,用switch语句表示DFA,但它不会接受它应该接受的单词。 我尝试添加一个单独的案例,将最终状态发送到输出“word accepted”或“word not accepted”,但这不起作用。 接受的示例词应为: 谷歌 扭动 XXGoooogleXeg

到目前为止,我的代码是:

public static void main(String[] args) {

    System.out.println("Enter a word to run on the DFA:");
    Scanner scanner = new Scanner(System.in);
    String string = scanner.nextLine();
    int state = 1;
    for (char s : string.toCharArray()) {
        switch (state) {
            case (1): {
                if (s == 'e' || s == 'l' || s == 'o' || s == 'x') {
                    state = 1;
                } else if (s == 'g') {
                    state = 2;
                }
            }
            break;
            case (2): {
                if (s == 'e' || s == 'l' || s == 'x') {
                    state = 1; {
                } if (s == 'o') {
                    state = 2;
                } else if (s == 'g') {
                    state = 3;
                }  
                }
            }
            break;
            case (3): {
                if (s == 'e' || s == 'x') {
                    state = 1; {
                } if (s == 'g' || s == 'o') {
                    state = 2;
                } else if (s == 'l') {
                    state = 4;
                }
            }
            break; }
            case (4): {
                if (s == 'g' || s == 'l' || s == 'o' || s == 'x') {
                    state = 1;
                } else if (s == 'e') {
                    state = 5;
            }
            break; }
            case (5): {
                if (s == 'e' || s == 'g' || s == 'l' || s == 'o' || s == 'x') {
                    state = 5;
                } else {
                    state = 5;
                }
            break; }

        }
    }

    if (state == 5) {
        System.out.println("Word accepted");
    } else {
        System.out.println("Word not accepted");
        scanner.close();
    }

}

注:我知道else语句是否很慢,但对于这样的小程序来说,它似乎足够快。

您的代码中有很多问题。试着正确缩进代码,看看在哪里打开和关闭括号

例如:

  • 在案例1和案例2中,您的休息时间在案例块之外
  • 在案例2和案例3中,在该语句
    state=1之后有一个空块;{}
  • 在案例2和案例3中,您的
    if else if
    包含在第一个
    if
    语句中。考虑到您的输入和预期输出,我假设这是错误的
以下是有效的格式化代码:

for (char s : string.toCharArray()) {
        switch (state) {
        case (1): {
            if (s == 'e' || s == 'l' || s == 'o' || s == 'x') {
                state = 1;
            } else if (s == 'g') {
                state = 2;
            }
            break;
        }

        case (2): {
            if (s == 'e' || s == 'l' || s == 'x') {
                state = 1;
            } else if (s == 'o') {
                state = 2;
            } else if (s == 'g') {
                state = 3;
            }

            break;
        }

        case (3): {
            if (s == 'e' || s == 'x') {
                state = 1;
            } else if (s == 'g' || s == 'o') {
                state = 2;
            } else if (s == 'l') {
                state = 4;
            }

            break;
        }
        case (4): {
            if (s == 'g' || s == 'l' || s == 'o' || s == 'x') {
                state = 1;
            } else if (s == 'e') {
                state = 5;
            }
            break;
        }
        case (5): {
            if (s == 'e' || s == 'g' || s == 'l' || s == 'o' || s == 'x') {
                state = 5;
            } else {
                state = 5;
            }
            break;
        }

        }
    }

这是一个正在运行的程序吗?在案例2和案例3中,您有大括号问题。您了解DFA是如何工作的吗?你了解DFA是如何工作的吗?你希望它能产生什么?当然!我现在明白了,非常感谢你的帮助:)是的,gpasch我了解DFA的工作方式为什么,你会用不同的方式来做?