2012-01-22 40 views
0

我試圖做的是日文字符轉換爲英文字符的程序,但我有一個奇怪的問題,我不能找出如何解決這個問題,我沒有發現什麼引起的雖然。有問題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; 
+3

好像你解決你自己的問題。剩下的問題是什麼? – ziesemer

+0

剩下的問題是,不管我是否使用如果或其他如果,總是不轉換的東西,我太愚蠢了,爲什麼 – user1071461

+1

如果你使用所有的Ifs,那麼你可能會覆蓋你的價值找到你的第一個如果落入另一個如果,其中你也設置「fromtype」的值。我對你使用的CharacterTable不太熟悉,但我也懷疑你可能會得到更準確的結果,只需要有三個數組或列表romaji,平假名和片假名,並循環遍歷每個數組以找出哪個數組通過了字符是在一個明確的==而不是「喜歡」。應該不是性能問題,因爲這些數組或列表會有多小。 – CptSupermrkt

回答

0

你需要考慮,如果一個字符匹配多種類型會發生什麼。有可能不管ifif else,您選擇fromtype在數據庫中沒有匹配的結果。

你可以考慮某種形式的評分機制,在這裏查詢每個類型,如果該字符匹配多種類型的,你考慮哪些類型有可用的替代品。

+0

它不應該能夠,它是每個字符的字符,所以在另一個函數中,我已經寫了一些東西,例如當日文字符包含多個英文字母時它會將其過濾掉(例如,它會發送例如「ta」作爲字符到這個函數並將其轉換) – user1071461

0

您認爲DataTable.Select()在不匹配時返回null。它不,它返回一個空數組。您的測試應該是這樣的:

if (CharacterTable.Select("blabla").Length > 0) { // etc.. }