我试图做的是日文字符转换为英文字符的程序,但我有一个奇怪的问题,我不能找出如何解决这个问题,我没有发现什么引起的虽然。有问题if和else if,C#
我使用的数据集,我使用此代码字符
转换我的问题是,它不希望某些字符转换,字符不想要转换为基于我在这里设置:
// Sets fromtype to the type the character(s) currently is/are
string fromtype = "";
if (CharacterTable.Select("Romaji like '%" + character + "%'") != null)
{
fromtype = "Romaji";
}
else if (CharacterTable.Select("Hiragana like '%" + character + "%'") != null)
{
fromtype = "Hiragana";
}
else if (CharacterTable.Select("Katakana like '%" + character + "%'") != null)
{
fromtype = "Katakana";
}
如果我改变每一个人行“如果”,那么它并不想承认罗马字的字符,如果我将其设置为“否则,如果”像现在,它看到。
下面是完整的代码:
DataSet CharacterDatabase = new DataSet();
DataTable CharacterTable = CharacterDatabase.Tables.Add();
//-- Add columns to the data table
CharacterTable.Columns.Add("Romaji", typeof(string));
CharacterTable.Columns.Add("Hiragana", typeof(string));
CharacterTable.Columns.Add("Katakana", typeof(string));
//-- Add rows to the data table
CharacterTable.Rows.Add("a", "?", "?");
// Sets fromtype to the type the character(s) currently is/are
string fromtype = "";
if (CharacterTable.Select("Romaji like '%" + character + "%'") != null)
{
fromtype = "Romaji";
}
else if (CharacterTable.Select("Hiragana like '%" + character + "%'") != null)
{
fromtype = "Hiragana";
}
else if (CharacterTable.Select("Katakana like '%" + character + "%'") != null)
{
fromtype = "Katakana";
}
// generates a new variable to store the return in
DataRow[] filteredRows = CharacterTable.Select(fromtype + " like '%" + character + "%'");
// Return the converted character in the requested type
foreach (DataRow row in filteredRows)
{
if (RequestedCharType == 1)
{
return row["Romaji"].ToString();
}
if (RequestedCharType == 2)
{
return row["Hiragana"].ToString();
}
if (RequestedCharType == 3)
{
return row["Katakana"].ToString();
}
}
// if it couldn't find the character, return the original character
return character;
好像你解决你自己的问题。剩下的问题是什么? – ziesemer
剩下的问题是,不管我是否使用如果或其他如果,总是不转换的东西,我太愚蠢了,为什么 – user1071461
如果你使用所有的Ifs,那么你可能会覆盖你的价值找到你的第一个如果落入另一个如果,其中你也设置“fromtype”的值。我对你使用的CharacterTable不太熟悉,但我也怀疑你可能会得到更准确的结果,只需要有三个数组或列表romaji,平假名和片假名,并循环遍历每个数组以找出哪个数组通过了字符是在一个明确的==而不是“喜欢”。应该不是性能问题,因为这些数组或列表会有多小。 – CptSupermrkt