我正在尝试构建一个自动机,我已经运行了几次,并且代码中似乎没有问题,但是仍然没有得到想要的结果,即“ CurrentState”应为X3
我提供的输入如下
string[] input = new string[4] { "a", "a", "b", "z" };
我将简化转换。但是,下表如下所示 我给A,A,B应该导致X3 X1 A X1 ---------- X1 A X1 ------------ X1 B X3 ------(卡在X1上)我已经干了运行代码同样,它应该属于x3 过渡表如下
states2 token actions output
x1 a x1
x1 b x3
x2 a x2
x2 b x1
x3 a x3
x3 b x4
x4 a x2
x4 b x3
这是代码
for (k = 0; k < input.Length; k++) //It is 4
{
if (input[k] == "z")
{
Console.WriteLine(currentState);
break;
// Environment.Exit(0);
}
else
{
for (j = 0; j < tokenActions.Length; j++) //its 8 and same as states and output
{
if (currentState == states2[j] && input[k] == tokenActions[j])
{
currentState = output[j];
}
}
}
}
更新:我注意到状态仅更改了第一个令牌的ONCE,之后保持不变。 (我的观察,可能是错误的)
答案 0 :(得分:3)
您说文件阅读器出现错误,但是代码中仍然存在错误。
在这里,我已经对您的代码进行了检测,以便其操作更加清晰。在运行该程序之前,预测其输出应为。然后运行程序。输出正确吗?
public static void Main()
{
string currentState = "x1";
string[] input = {"a", "a", "b", "z"};
string[] states2 = {"x1", "x1", "x2", "x2", "x3", "x3", "x4", "x4"};
string[] tokenActions = {"a", "b", "a", "b", "a", "b", "a", "b" };
string[] output = {"x1", "x3", "x2", "x1", "x3", "x4", "x2", "x3"};
for (int k = 0; k < input.Length; k++) //It is 4
{
if (input[k] == "z")
{
Console.WriteLine("final state is {0}", currentState);
break;
// Environment.Exit(0);
}
else
{
Console.WriteLine("new input {0}", input[k]);
for (int j = 0; j < tokenActions.Length; j++) //its 8 and same as states and output
{
if (currentState == states2[j] && input[k] == tokenActions[j])
{
Console.WriteLine("input is {0} current state is {1} new state is {2}", input[k], currentState, output[j]);
currentState = output[j];
}
}
}
}
}
现在您看到错误了吗?
问问自己:当我最初编写代码时,如何防止这个错误?您对如何更清楚地编写代码,从而无法编写此错误有什么想法?