我似乎無法將值添加到字典中。我正在嘗試遞回擴展字典,但不斷收到錯誤/警告:“無法從 'void' 轉換為 'System.Collections.Generic.Dictionary<string, bool>'”。我是初學者,所以我不確定如何解決這個錯誤。
我在 return 陳述句的最后一行收到錯誤,特別是 model.Add(p,true) 部分。

namespace InferenceEngine
{
internal class TruthTable
{
TruthTable() { }
private List<string> ExtractSymbol(Sentence KB)
{
List<string> result = new List<string>();
if(KB.symbol != null)
{
result.Add(KB.symbol);
}
else
{
foreach(Sentence s in KB.child)
{
result.Concat(ExtractSymbol(s));
}
}
return result;
}
private bool PL_True(Sentence KB, Dictionary<string,bool> model)
{
if(KB.symbol != null)
{
return model[KB.symbol];
}
else if ( KB.connetives == "AND")
{
foreach (Sentence s in KB.child)
{
if(PL_True(s, model) == false)
{
return false;
}
return true;
}
}
else if (KB.connetives == "OR")
{
foreach (Sentence s in KB.child)
{
if (PL_True(s, model) == true)
{
return true;
}
return false;
}
}
else if (KB.connetives == "AND")
{
foreach (Sentence s in KB.child)
{
if (PL_True(s, model) == false)
{
return false;
}
return true;
}
}
else if (KB.connetives == "IF")
{
Sentence left = KB.child[0];
Sentence right = KB.child[KB.child.Count - 1];
if (PL_True(left,model) == true || PL_True(right,model) == false)
{
return false;
}
return true;
}
else if (KB.connetives == "IFF")
{
Sentence left = KB.child[0];
Sentence right = KB.child[KB.child.Count - 1];
if (PL_True(left, model) == true && PL_True(right, model) == false)
{
return false;
}
return true;
}
else if (KB.connetives == "NOT")
{
Sentence opposite = KB.child[0];
if (PL_True(opposite, model) == true)
{
return false;
}
return true;
}
return false;
}
public bool TT_Entails(Sentence KB, Sentence Alpha)
{
List<string> symbol1 = ExtractSymbol(KB);
List<string> symbol2 = ExtractSymbol(Alpha);
List<string> symbols = new List<string>();
symbols.Concat(symbol1);
symbols.Concat(symbol2);
Dictionary<string, bool> table = new();
return TT_Check_All(KB, Alpha, symbols, table );
}
private bool TT_Check_All(Sentence KB, Sentence Alpha, List<string> symbol, Dictionary<string, bool> model)
{
if(symbol.Count == 0)
{
if(PL_True(KB, model) == true)
{
return PL_True(Alpha, model);
}
return false;
}
string p = symbol[0];
symbol.RemoveAt(0);
List<string> rest = symbol;
return TT_Check_All(KB, Alpha, rest, model.Add(p,true)) && TT_Check_All(KB, Alpha, rest, model.Add(p, false));
}
}
}
uj5u.com熱心網友回復:
Add字典上的方法回傳void. 因此,當您將結果遞回地傳遞model.Add(p, false)到時,您傳遞的是 a而不是預期的型別。TT_Check_AllvoidDictionary<string, bool>
Add您可以通過在遞回呼叫之前執行并傳入模型來解決此問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/495648.html
下一篇:如何從飛鏢的嵌套串列中提取專案
