表示 DFA Java 的 Switch 语句错误

Switch statement error representing DFA Java

我已经编写了一个 Java 程序来使用 switch 语句表示 DFA,但它不会接受它应该做的词。 我尝试添加一个单独的案例以将最终状态发送到输出 'word accepted' 或 'word not accepted' 但这没有用。 接受的示例词应该是: google 格格 xxgooooooglexeg

到目前为止我的代码:

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();
    }

}

P.S: 我知道 if 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;
        }

        }
    }