首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Java的Automaton DFA实现不起作用

使用Java的Automaton DFA实现不起作用
EN

Stack Overflow用户
提问于 2018-10-07 03:36:43
回答 1查看 142关注 0票数 2

我现在正在我的大学学习DFA和NFA自动机,以及如何使用Java代码实现其中的一些自动机。

我在这个练习中遇到了一些问题:我们有4个不同的实验室轮次(T1,T2,T3和T4),我们需要编写代码来识别特定的字符串(由学生的大学徽章编号和他的名字组成,例如123321Johnson)是否对应于T2或T3。

我们知道:

  • T1是在"A“和”K“之间的偶数徽章编号和姓氏的轮换
  • T2是在"A”和“K”之间的徽章编号和姓氏的偶数轮换
  • T3是在"L“和”Z“之间的徽章编号和姓氏的奇数轮换
  • T4是在"L”和“Z”之间的徽章编号和姓氏的偶数轮换<

>F210

我们还知道字符串必须由至少一个数字和至少一个字母组成。

例如,自动机必须接受"1232324Gac""1232323Lum",但不能接受"121234Lum""121233Gac"

下面是我写的代码:

代码语言:javascript
复制
import java.util.Scanner;

public class Es3 {

    static Scanner sc = new Scanner(System.in);
    String s = sc.next();
    public static boolean scan(String s)
    {
        int state = 0;                      
        int i = 0;                              

        while (state >= 0 && i < s.length()) {
            final char ch = s.charAt(i++);
            switch (state) {
            case 0:
                if (ch >= 0 && ch <= 9)
                    state = 1;
                else
                    state = -1;
                break;

            case 1:
                if (ch >=0 && ch <=9)
                    state = 1;
                else if (ch >='a' && ch <='k')
                    if ((s.charAt(i--))%2==0)
                        state = 2;
                    else
                        state = -1;
                else if (ch >='l' && ch <='z')
                    if ((s.charAt(i--))%2==1)
                        state = 3;
                    else
                        state = -1;
                else
                    state = -1;
                break;

            case 2:
                if (ch >='a' && ch <='z')
                    state = 2;
                else
                    state = -1;
                break;

            case 3:
                if (ch >='a' && ch <='z')
                    state = 3;
                else 
                    state = -1;
                break;
            }
        }
        return (state == 2 || state == 3);      
    }

    public static void main(String[] args)
    {
        System.out.println(scan(args[0]) ? "OK" : "NO");
    }
}

显然,代码不起作用,但这对于显示练习的一般目的很重要。

有人能帮帮我吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-10-07 05:13:27

您的算法不起作用的原因是您试图将char值与int值进行比较,这不会给出预期的结果。此外,当您检查char值是否在某个字母范围内时,您没有考虑大写字母。

代码语言:javascript
复制
import java.util.Scanner;

public class Es3 
{
    static Scanner sc = new Scanner(System.in);
    String s = sc.next();
    public static boolean scan(String s)
    {
        int state = 0;
        int i = 0;

        while (state >= 0 && i < s.length()) {
            final char ch = s.charAt(i++);
            switch (state) {
            case 0:
                // Compare the char to the char values of the numbers
                if (ch >= '0' && ch <= '9')
                    state = 1;          
                else
                    state = -1;
                break;

            case 1:  
                // Same here, compare the char to the char values of the numbers
                if (ch >= '0' && ch <= '9')
                    state = 1;
                // Check if the char is capital, as well as lowercase
                else if ((ch >= 'a' && ch <= 'k') || (ch >= 'A' && ch <= 'K'))
                    // Convert the char to an int before performing the calculations
                    if ((Character.getNumericValue(s.charAt(i-1)))%2 == 0)
                        state = 2;
                    else
                        state = -1;
                // Check if the char is capital as well
                else if ((ch >= 'l' && ch <= 'z') || (ch >= 'L' && ch <= 'Z'))
                    // Convert from char to int before calculating
                    if ((Character.getNumericValue(s.charAt(i-1)))%2 == 1)
                        state = 3;
                    else
                        state = -1;
                else
                    state = -1;
                break;

            case 2:
                // Check if the char is capital as well
                if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
                    state = 2;
                else
                    state = -1;
                break;

            case 3:
                // Check if the char is capital as well
                if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
                    state = 3;
                else 
                    state = -1;
                break;
            }
        }
        System.out.println("State "+state);
        return (state == 2 || state == 3);      
    }

    public static void main(String[] args)
    {
        System.out.println(scan(args[0]) ? "OK" : "NO");
    }
}

我认为上面的代码应该能做你想做的事情。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52682702

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档