表示DFA Java的Switch语句错误



我已经编写了一个Java程序来使用switch语句表示DFA,但它不会接受它应该做的单词。我尝试添加一个单独的案例来将最终状态发送到,以输出"单词已接受"或"单词未接受",但这不起作用。接受的单词示例应为:谷歌gglexxgooooooglexeg

到目前为止我的代码:

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:我知道else语句是否很慢,但对于这样的小程序来说,它似乎足够快了。

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

例如:

  • 在案例1和案例2中,您的休息时间在案例块之外
  • state = 1; {}语句之后的情况2和情况3中有一个空块
  • 在情况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;
        }
        }
    }

最新更新