0

帮助先生,我正在尝试做一些简单的dfa,但它不能正常工作。我试图从包含 111、000,110 的文本文件中测试 3 行输入。我试图将它们分成 3 个令牌,但在测试时,程序仍然作为一个整体运行它。提前感谢您的帮助

 import java.util.*;
 import java.io.*;

 public class A
 {
 public static void main(String args[]) throws Exception
 {
    FileReader fileInput = new FileReader("B:\\input1.txt");
    BufferedReader readInput = new BufferedReader(fileInput);

    FileWriter fileOutput = new FileWriter("B:\\output.txt");
    PrintWriter outFile = new PrintWriter(fileOutput);

    Scanner scanLine = new Scanner(readInput);
    int cs = 0;
    final int q0 = 0;
    final int q1 = 1;

    int noOfLines = 0;

    while(scanLine.hasNextLine())
    {
        String[] tokens = scanLine.next().split("\n");

        for(String words : tokens)
        {
          outFile.println(words);
          char[] stringArray = words.toCharArray();

        for(char x : stringArray)
        {
            switch(cs)
            {
                case q0:
                    switch(x)
                    {
                        case '1':
                        cs = q1;
                        break;

                        case '0':
                        cs = q0;
                        break;

                        default:
                        outFile.println("error");
                        break;
                    }
                break;

                case q1:
                    switch(x)
                    {
                        case '1':
                        cs = q1;
                        break;

                        case '0':
                        cs = q1;
                        break;

                        default:
                        outFile.print("error");
                        break;
                    }
                break;

                default:
                break;
            }
        }//end for char array
        outFile.println();
        noOfLines++;
        if(cs == q1)
        {
        outFile.println("accepted");
        }
        else
        {
        outFile.println("rejected");
        }//end if else
        }//end for string array
    }//end while

    outFile.println();

    fileInput.close();
    readInput.close();
    outFile.close();
    fileOutput.close();
  }

}
4

1 回答 1

0

您的 cs 变量必须是 String Datatype 而不是 Integer,这似乎是一个错误。

这是一个例子。

public class StringSwitchDemo {

    public static int getMonthNumber(String month) {

        int monthNumber = 0;

        if (month == null) {
            return monthNumber;
        }

        switch (month.toLowerCase()) {
            case "january":
                monthNumber = 1;
                break;
            case "february":
                monthNumber = 2;
                break;
            case "march":
                monthNumber = 3;
                break;
            case "april":
                monthNumber = 4;
                break;
            case "may":
                monthNumber = 5;
                break;
            case "june":
                monthNumber = 6;
                break;
            case "july":
                monthNumber = 7;
                break;
            case "august":
                monthNumber = 8;
                break;
            case "september":
                monthNumber = 9;
                break;
            case "october":
                monthNumber = 10;
                break;
            case "november":
                monthNumber = 11;
                break;
            case "december":
                monthNumber = 12;
                break;
            default: 
                monthNumber = 0;
                break;
        }

        return monthNumber;
    }

    public static void main(String[] args) {

        String month = "August";

        int returnedMonthNumber =
            StringSwitchDemo.getMonthNumber(month);

        if (returnedMonthNumber == 0) {
            System.out.println("Invalid month");
        } else {
            System.out.println(returnedMonthNumber);
        }
    }
}
于 2012-07-16T09:51:41.233 回答