2016-09-29 47 views
0

我試圖檢查用戶輸入的內容是否在字母數組中。然後將該字母轉換成莫爾斯碼,這是摩爾斯陣列。我必須使用char數組來按順序顯示用戶輸入,但它會按字母順序顯示莫爾斯碼。如何停止正確顯示它?提前致謝。針對不同陣列檢查字符數組vb.net

Dim strCode As String = txtCode.Text.ToUpper 'What the user enters must be letters it can also be - or = 
    Dim strText() As Char = strCode.ToCharArray 

    Dim strLetter() As String = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"} 
    Dim strMorse() As String = {"*=", "=***", "=*=*", "=**", "*", "**=*", "==*", "****", "**", "*===", "=*=", "*=**", "==", "=*", "===", "*==*", "==*=", "*=*", "***", "=", "**=", "***=", "*==", "=**=", "=*==", "==**"} 

    For Each letter As Char In strText 
     For x As Integer = 0 To strLetter.Length - 1 
      If strCode.Contains(strLetter(x)) Then 
       MessageBox.Show(strMorse(x)) 
      End If 
     Next 
    Next 
End Sub 

回答

2

只需使用字典並遍歷字符串中的所有字符,您就不必擔心查找字符並按正確的順序翻譯它們。用拼音字母的鍵,莫爾斯電碼作爲值......那麼你可以去通過文字和翻譯這樣每個字母:

Dim translate As New Dictionary(Of String, String) From {{" ", " "}, _ 
                  {"A", "*="}, _ 
                  {"B", "=***"}, _ 
                  {"C", "=*=*"}, _ 
                  {"D", "=**"}, _ 
                  {"E", "*"}, _ 
                  {"F", "**=*"}, _ 
                  {"G", "==*"}, _ 
                  {"H", "****"}, _ 
                  {"I", "**"}, _ 
                  {"J", "*==="}, _ 
                  {"K", "=*="}, _ 
                  {"L", "*=**"}, _ 
                  {"M", "=="}, _ 
                  {"N", "=*"}, _ 
                  {"O", "==="}, _ 
                  {"P", "*==*"}, _ 
                  {"Q", "==*="}, _ 
                  {"R", "*=*"}, _ 
                  {"S", "***"}, _ 
                  {"T", "="}, _ 
                  {"U", "**="}, _ 
                  {"V", "***="}, _ 
                  {"W", "*=="}, _ 
                  {"X", "=**="}, _ 
                  {"Y", "=*=="}, _ 
                  {"Z", "==**"}} 
    Dim translatedMsg As String = Nothing 
    For Each c As Char In txtCode.Text.ToUpper 
     translatedMsg += translate(c) & " " 
    Next 
    Debug.Print(translatedMsg.TrimEnd) 

    'Output for "Hello World": **** * *=** *=** === *== === *=* *=** =** 

編輯:好了,去從價值到價值的倒退並不像從價值到價值那麼幹淨,因爲它沒有原生的功能,但是Linq對此很好......如果我說實話,我沒有想到首先翻譯的空間。我可能會爲字典中的空格添加一個特殊字符,這會使翻譯變得更容易。但是,儘管如此,爲了示例的目的,我會堅持我的第一個例子,這裏是如何將消息翻譯回來的。我再次使用了「Hello World」,爲了解決空間問題,我只是用空格替換了下劃線,這樣我就可以正確解碼消息了(另外,顯然你會失去大寫/小寫區分,因爲莫爾斯碼不區分兩個):

Dim strMorseMsg As String = "**** * *=** *=** === *== === *=* *=** =**" 

    strMorseMsg = strMorseMsg.Replace(" ", " _ ") 'substitution here to distinguish spaces between words from the space between letters 
    Dim aryMorseMsg() As String = strMorseMsg.Split(CChar(" ")) 

    Dim originalMessage As String = Nothing 
    For Each code As String In aryMorseMsg 
     If code.Equals("_") Then 'here's where the underscore helps branch our logic between adding a space and adding a letter 
      originalMessage += " " 
     Else 
      originalMessage += translate.FirstOrDefault(Function(x) x.Value = code).Key 
     End If 
    Next 
    MsgBox(originalMessage) 

    'Outputs "HELLO WORLD" 
+0

很好的解決方案,這正是一本字典是什麼。請注意,您在字母之間也需要空格。否則,收件人將不知道'***'是指「EEE」,「EI」,「IE」還是「S」。 – Heinzi

+0

哦,是的,非常好的一點! – soohoonigan

+0

@Heinzi我第二!用字典,我可以很容易地知道代碼的重點,並立即知道如何檢查翻譯,如果我覺得有需要。 –

2

如果您使用簡單的步驟(如「BA」),您可以看到邏輯錯誤。你永遠不會將外部循環中的字母與內部中的字母進行比較。相反,你的If語句問「嗨,我的輸入是否包含我的strLetter數組中的第一個字母?」是的,「A」在那裏(即使它不是第一個字符)。

如果字母在範圍內(在A和Z之間),對於賦值的更簡單的解決方案將使用字母的ASCII值從「A」的偏移量。這消除了定義strLetter的需要。您還可以省略將輸入文本轉換爲字符數組。一個字符串已經讓你枚舉字符。

Dim strCode As String = txtCode.Text.ToUpper 'What the user enters must be letters it can also be - or = 

Dim strMorse() As String = {"*=", "=***", "=*=*", "=**", "*", "**=*", "==*", "****", "**", "*===", "=*=", "*=**", "==", "=*", "===", "*==*", "==*=", "*=*", "***", "=", "**=", "***=", "*==", "=**=", "=*==", "==**"} 

For Each letter As Char In strCode 
    If letter >' "A"c AndAlso letter <= "Z"c Then 
    MessageBox.Show(strMorse(AscW(letter) - AscW("A"c))) 
    End If 
Next 
+0

謝謝。我會嘗試的。所有這些仍然是新的,所以幫助表示讚賞。 – StudentNeedsHelp

0
Sub Main() 
    Dim codes() As String = { _ 
     "*=", "=***", "=*=*", "=**", "*", "**=*", "==*", "****", "**", _ 
     "*===", "=*=", "*=**", "==", "=*", "===", "*==*", "==*=", "*=*", _ 
     "***", "=", "**=", "***=", "*==", "=**=", "=*==", "==**" _ 
    } 

    Dim input As String = Console.ReadLine().ToUpper() 

    ' Convert characters to values that can be used as indexes in the code array 
    Dim indexes = input.[Select](Of Integer)(Function(l As Char) Asc(l) - 65) 
    ' limit the values to based on the range of possible values 
    Dim boundsCheck = indexes.Where(Function(l As Integer) l >= 0 And l <= 26) 
    ' map the valid inputs to codes fro myour array 
    Dim outCodes = boundsCheck.[Select](Of String)(Function(l As Integer) codes(l)) 
    ' concatenate the codes into a string and display 
    Console.WriteLine(String.Join(" ", outCodes)) 
End Sub