C# 中与 DataSet 相关的奇怪问题
我目前正在开发一个程序,可以将日语字符转换为英语字符,反之亦然。 但它并没有发挥多大作用,在过去的几天里,我一直在尽一切努力让它发挥作用,这可能是一些小愚蠢的问题,但我就是找不到它。我对这一切都很陌生,所以非常感谢您的帮助。
现在的问题是它只想转换罗马字字符,但是如果更改一些代码,更具体地说,如果我将以下内容从“if”更改为 else if,那么它会转换平假名和片假名,但不会转换罗马字。
string fromtype = "";
// Determines what type the character is currently
// && fromtype == "" added to avoid weird unexplainable errors...
if (CharacterTable.Select("Romaji = '" + character + "'") != null && fromtype == "")
{
fromtype = "Romaji";
}
else if (CharacterTable.Select("Hiragana = '" + character + "'") != null && fromtype == "")
{
fromtype = "Hiragana";
}
else if (CharacterTable.Select("Katakana = '" + character + "'") != null && fromtype == "")
{
fromtype = "Katakana";
}
我什至尝试过删除这个尝试自动查找角色类型并使用单选按钮执行此操作的功能,以便用户必须选择它,但不知怎的,它似乎做了几乎相同的事情......所以我现在完全困惑了,任何帮助都非常多 欢迎。
这是完整的代码:
public string CheckCharacter(string character, int RequestedCharType)
{
// RequestedCharType
// 1 = Romaji
// 2 = Hiragana
// 3 = Katakana
//-- Instantiate the data set and table
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", "あ", "ア");
CharacterTable.Rows.Add("i", "い", "イ");
// Sets fromtype to the type the character(s) currently is/are
string fromtype = "";
// Determines what type the character is currently
// && fromtype == "" added to avoid weird unexplainable errors...
if (CharacterTable.Select("Romaji = '" + character + "'") != null && fromtype == "")
{
fromtype = "Romaji";
}
else if (CharacterTable.Select("Hiragana = '" + character + "'") != null && fromtype == "")
{
fromtype = "Hiragana";
}
else if (CharacterTable.Select("Katakana = '" + character + "'") != null && fromtype == "")
{
fromtype = "Katakana";
}
// generates a new variable to store the return in
DataRow[] filteredRows = CharacterTable.Select(fromtype + " = '" + 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;
}
I'm currently working on a program that converts japanese characters to english characters and the other way around.
It's not working much though, For the last few days I've been trying everything to try and get it to work, it's probably some small dumb problem but I just can't find it. I'm pretty new to all this so any help is appreciated.
Now the problem is that it only wants to convert Romaji characters, however if change some code, more specifically if I change the following from "if" to else if, then it converts hiragana and katakana but NOT romaji..
string fromtype = "";
// Determines what type the character is currently
// && fromtype == "" added to avoid weird unexplainable errors...
if (CharacterTable.Select("Romaji = '" + character + "'") != null && fromtype == "")
{
fromtype = "Romaji";
}
else if (CharacterTable.Select("Hiragana = '" + character + "'") != null && fromtype == "")
{
fromtype = "Hiragana";
}
else if (CharacterTable.Select("Katakana = '" + character + "'") != null && fromtype == "")
{
fromtype = "Katakana";
}
I've even tried removing this function that tries to automatically find what type the character is and do it with radiobuttons so that the user has to select it, but somehow it seems to do pretty much the same thing... So I'm totally confused at this point, any help is very much welcome.
Here is the full code:
public string CheckCharacter(string character, int RequestedCharType)
{
// RequestedCharType
// 1 = Romaji
// 2 = Hiragana
// 3 = Katakana
//-- Instantiate the data set and table
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", "あ", "ア");
CharacterTable.Rows.Add("i", "い", "イ");
// Sets fromtype to the type the character(s) currently is/are
string fromtype = "";
// Determines what type the character is currently
// && fromtype == "" added to avoid weird unexplainable errors...
if (CharacterTable.Select("Romaji = '" + character + "'") != null && fromtype == "")
{
fromtype = "Romaji";
}
else if (CharacterTable.Select("Hiragana = '" + character + "'") != null && fromtype == "")
{
fromtype = "Hiragana";
}
else if (CharacterTable.Select("Katakana = '" + character + "'") != null && fromtype == "")
{
fromtype = "Katakana";
}
// generates a new variable to store the return in
DataRow[] filteredRows = CharacterTable.Select(fromtype + " = '" + 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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题在于您对
Select
工作方式的误解。当没有匹配项时,Select
不会返回null
,因此您的第一个if
始终为 true。相反,您需要检查是否有任何可以使用Enumerable.Any()
执行的结果(添加using System.Linq
):或者您可以检查数组长度:
fromType == ""
位的全部内容肯定是不需要的。enum
类型。switch
语句而不是if (RequestedCharType == 1)
&c.Your problem is in your misunderstanding of how
Select
works.Select
does not returnnull
when there are no matches so your firstif
is always true. Instead you need to check whether there were any results which you can do withEnumerable.Any()
(addusing System.Linq
):Alternatively you can check the array length:
fromType == ""
bit is all about this is surely not needed.enum
type for your char types.switch
statement instead ofif (RequestedCharType == 1)
&c.