我正在尝试检查用户输入的是否在字母数组中。然后将这封信翻译成摩尔斯电码莫尔斯电码。我必须使用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
答案 0 :(得分: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”,为了解决空间问题,我只是用空格代替空格,这样我就可以正确地解码消息了(另外,显然你失去了大写/小写的区别,因为莫尔斯代码没有区分2):
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"
答案 1 :(得分: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
答案 2 :(得分: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