首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >根据不同的数组vb.net检查字符数组

根据不同的数组vb.net检查字符数组
EN

Stack Overflow用户
提问于 2016-09-30 02:41:14
回答 3查看 363关注 0票数 0

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

代码语言:javascript
运行
复制
    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
EN

回答 3

Stack Overflow用户

发布于 2016-09-30 03:07:45

只需使用字典并遍历字符串中的所有字符,您就不必担心如何查找字符并按正确的顺序进行翻译。使用字母表字母作为关键字,使用摩尔斯电码作为value...then,您可以像这样浏览文本并翻译每个字母:

代码语言:javascript
运行
复制
    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编辑:嗯,从值到键的倒退不如从键到键的整洁,因为它没有本机函数,但Linq在that...and上工作得很好。老实说,我一开始并没有考虑到空格的转换。我可能会在字典中添加一个特殊的空格字符,这将使翻译变得容易得多。但尽管如此,出于示例的原因,我将继续使用我的第一个示例,下面是如何将消息翻译回来的。我再次使用了"Hello World“,为了解决空格问题,我只是用下划线代替了空格,这样我就可以正确地解码消息(另外,很明显你会失去大小写的区别,因为摩尔斯电码不能区分这两者):

代码语言:javascript
运行
复制
    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"
票数 2
EN

Stack Overflow用户

发布于 2016-09-30 03:11:35

如果你使用简单的东西遍历你的代码,比如"BA",你可以看到逻辑错误。您永远不会将外部循环中的字母与内部循环中的任何内容进行比较。相反,If语句将询问“嘿,我的输入是否包含strLetter数组中的第一个字母?”是的,"A“在其中(尽管它不是第一个字符)。

一种更简单的赋值解决方案是,如果字母在范围内(在A和Z之间),则使用字母的ASCII值与"A“的偏移量。这消除了定义strLetter的需要。您还可以省略将输入文本转换为字符数组。字符串已经允许您枚举字符。

代码语言:javascript
运行
复制
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
EN

Stack Overflow用户

发布于 2016-09-30 03:30:51

代码语言:javascript
运行
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39777734

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档