【问题标题】:check char array against a different array vb.net针对不同的数组 vb.net 检查 char 数组
【发布时间】:2016-09-29 18:41:14
【问题描述】:

我正在尝试检查用户输入的内容是否在字母数组中。然后将字母转换为莫尔斯电码,即莫尔斯数组。我必须使用 char 数组按顺序显示用户输入,但它按字母顺序显示莫尔斯电码。如何正确停止显示?提前致谢。

    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

【问题讨论】:

    标签: arrays vb.net loops


    【解决方案1】:

    如果您使用简单的代码(例如“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
    

    【讨论】:

    • 谢谢。我会试试的。对这一切仍然很陌生,因此感谢您的帮助。
    【解决方案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"
    

    【讨论】:

    • 不错的解决方案,这正是字典的用途。请注意,字母之间也需要空格。否则,收件人将不知道*** 是指EEEEIIE 还是S
    • @Heinzi 我支持这个!有了字典,我可以很容易地分辨出代码的意义,并且如果我觉得有必要,我会立即知道如何检查翻译。
    • 我应该在哪里添加空格,以便将 *** 识别为“S”?
    • 我怎样才能从莫尔斯电码转换成字母,因为如果循环遍历 char 它会单独检查每个字符。
    • 你看过编辑吗?它不是遍历字符,而是将消息分成字符组并遍历这些组。
    【解决方案3】:
    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
    

    【讨论】:

      猜你喜欢
      • 2021-01-26
      • 1970-01-01
      • 2020-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-19
      • 2019-09-13
      • 1970-01-01
      相关资源
      最近更新 更多