我正在尝试获取用户输入并将其放入一维数组(我现在知道有更好的方法将用户输入放入数组中,我可以稍后清理它但现在它可以工作)每个元素一个字符。然后逐步完成从用户输入制作的数组,并在预定义的二维数组中查找该字符,元素3找到代表该字母的3“字母”颜色代码,元素0-2(稍后我将使用相同的数组解码)。问题是,我无法在二维数组中找到“空格”字符(如果我在输入中输入一个空格,它会在输出中被忽略)。我最初尝试将“”放入元素3但是没有用,所以我尝试了Char(32)并且也没有用,然后我试着将Char(32)显式地放入输入数组中,这也不起作用。编辑我尝试使用我用于Char(32)的两种方法尝试“”C而没有运气。我用手指搜索了一下,无法找到解决方法。我对VB很陌生,虽然我现在正在介绍VB类(这不是一个类项目,只是一个受生物实验室启发的副项目,通过串起彩色珠子编码一个句子来模拟遗传编码,尽管我可能会向生物指导员展示它)但是我们本周只有1维数组并且正在进行控制中断,2维数组将在一周或2中。这是我的代码,例如它,它是善良的由于做出改变试图让它发挥作用而被黑客攻击。
Module Module1
Sub Main()
Dim inputString As String = ""
Dim outputString As String = ""
Dim codeString() As String
Dim testString As String = ""
Dim decodeString() As String
Dim keyInput As ConsoleKeyInfo
Dim index(,) As String = New String(,) {{"Y", "G", "R", "c"}, {"Y", "R", "B", "e"}, {"R", "B", "Y", "h"}, _
{"R", "G", "Y", "i"}, {"R", "B", "G", "k"}, {"B", "R", "Y", "l"}, {"G", "Y", "B", "m"}, {"Y", "B", "G", "o"}, _
{"B", "Y", "G", "s"}, {"B", "R", "G", "t"}, {"G", "R", "B", "u"}, {"Y", "Y", "Y", Chr(32)}, _
{"G", "G", "G", """start"""}, {"R", "R", "R", """stop"""}}
' Allowed characters are c, e, h, i, k, l, m, o, s, t, u, "space", "start", "stop"
Dim count1 As Integer = 0
Dim count2 As Integer = 0
Dim count3 As Integer = 0
Dim maxVal As Integer = 0
Const quit As String = "Q"
Const code As String = "C"
Const decode As String = "D"
inputString = UCase(InputBox$("Enter C to code or D to decode, Q to quit: ")) 'convert to upper case to eliminate case sensitivity
If UCase(inputString) = code Then ' get user input
Console.WriteLine("Enter sentence or Q to quit (""start"" and ""stop"" will be automaticly appended :")
count1 = 0
Do While UCase(inputString) <> quit
keyInput = Console.ReadKey()
Console.WriteLine()
inputString = UCase(keyInput.Key.ToString) 'convert to upper case to eliminate case sensitivity
ReDim Preserve codeString(count1)
If codeString(count1) = Chr(32) Then
codeString(count1) = Chr(32)
Else
codeString(count1) = LCase(keyInput.Key.ToString) 'convert to lower case to eliminate case sensitivity
maxVal = UBound(codeString)
End If
count1 += 1
Loop
ReDim Preserve codeString(maxVal)
'codeString(count1 - 1) = inputEnd
ReDim Preserve codeString(maxVal)
count1 = 0
Console.ForegroundColor = ConsoleColor.Green
Console.Write(index(12, 0) & index(12, 1) & index(12, 2)) 'append "start" character
Console.ResetColor()
Console.Write(" / ")
While count1 <= maxVal ' look up input letter and output 3 color code
For count2 = 0 To 13 Step 1
If codeString(count1) = index(count2, 3) Then
For count3 = 0 To 2 Step 1 'set the colors
If index(count2, count3) = "B" Then
Console.ForegroundColor = ConsoleColor.Blue
ElseIf index(count2, count3) = "G" Then
Console.ForegroundColor = ConsoleColor.Green
ElseIf index(count2, count3) = "R" Then
Console.ForegroundColor = ConsoleColor.Red
ElseIf index(count2, count3) = "Y" Then
Console.ForegroundColor = ConsoleColor.Yellow
End If
Console.Write(index(count2, count3))
Next
Console.ResetColor() ' reset the colors
Console.Write(" / ")
End If
Next
count1 += 1
End While
Console.ForegroundColor = ConsoleColor.Red
Console.Write(index(13, 0) & index(13, 1) & index(13, 2)) 'append "start" character
Console.ResetColor()
End If
Console.ReadLine() ' keep output window open
End Sub
结束模块
答案 0 :(得分:0)
有趣的项目。
我很少再使用数组,而是选择像Dictionaries这样适合查找的数据结构。
这是一个使用LINQ表达式和几个词典的示例。 Dictionary.ContainsKey(&#34;&#34;)正常工作,以便解决原始问题。
Sub Main()
'allowed characters are c, e, h, i, k, l, m, o, s, t, u, "space", "a=start", "z=stop"
Dim codes = New Dictionary(Of String, String) From {
{"c", "YGR"},
{"e", "YRB"},
{"h", "RBY"},
{"i", "RGY"},
{"k", "RBG"},
{"l", "BRY"},
{"m", "GYB"},
{"o", "YBG"},
{"s", "BYG"},
{"t", "BRG"},
{"u", "GRB"},
{" ", "YYY"},
{"a", "GGG"}, 'start
{"z", "RRR"} 'stop
}
Dim colors = New Dictionary(Of String, ConsoleColor) From {
{"Y", ConsoleColor.Yellow},
{"B", ConsoleColor.Blue},
{"G", ConsoleColor.Green},
{"R", ConsoleColor.Red}
}
Select Case InputBox("Enter C to code or D to decode, Q to quit: ").ToUpper
Case "C" 'code
Console.WriteLine("Enter sentence or Q to quit (""start"" and ""stop"" will be automaticly appended :")
Dim sb = New Text.StringBuilder
sb.Append("a")
Do
With Console.ReadKey()
If .Key = ConsoleKey.Q Then
Exit Do
End If
If codes.ContainsKey(.KeyChar) Then
sb.Append(.KeyChar)
Console.WriteLine()
Else
Console.WriteLine("(invalid)")
End If
End With
Loop
sb.Append("z")
For Each c In String.Join(" / ", From term In sb.ToString Select codes(term))
If colors.ContainsKey(c) Then
Console.ForegroundColor = colors(c)
Else
Console.ResetColor()
End If
Console.Write(c)
Next
Console.ResetColor()
Console.ReadLine() ' keep output window open
Case "D" 'decode
End Select
End Sub