帮助先生,我正在尝试做一些简单的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();
}
}