我有一个Visual Basic的家庭作业程序。它为7个朋友存储7个并行阵列的电话号码。我正在使用array.indexof来查看我的输入字符串是否在名称数组中,然后返回完整的名称和相应的数字。我不是在寻找海报的答案,只是提供一些指导或者指出我的网站。提前致谢。抱歉,如果我的帖子出现乱码或格式错误,请第一次发布Robert
Dim intCounter As Integer
Dim strNames() As String = {"BILLY", "JILLY", "MILLY", "PHILLY", "LARRY", "CURLY", "MOE"}
Dim strNumbers() As String = {"313-213-1234", "248-123-3452", "123-321-1234", "987-986-3456", "567-635-7632", "524-456-6782", "918-872-3452"}
Dim strLookFor As String
Dim found As Boolean
strLookFor = InputBox("Type in the person you want to call", "Phone a Friend") 'prompt from user
strLookFor.TrimEnd() 'trim whitespace after entry
strLookFor = strLookFor.ToUpper 'convert to upper case to match
txtResults.Clear() 'clear text box
found = False 'set boolean to false
intCounter = 0 'set intCounter to 0
Do While Not found And intCounter < strNames.Length
Dim intIndex As Integer = Array.IndexOf(strNames, strLookFor) 'SHOULD return a value of 0 or higher if found
If intIndex >= 0 Then 'if loop for value of 0 or higher
found = True
End If
intCounter += 1 'add 1 to intCounter
Loop
If found Then 'display phone match up results
txtResults.Text = ((strNames(intCounter - 1)) & " " & (strNumbers(intCounter - 1)))
Else
txtResults.Text = ("Match not found.")
End If
End Sub
答案 0 :(得分:2)
我建议使用字典(字符串,字符串),它是这类事物的理想选择:
Dim dic As New Dictionary(Of String, String)
dic.Add("BILLY", "123-456-888")
dic.Add("JILLY", "333-555-222")
dic.Add("MILLY", "777-334-667")
dic.Add("PHILLY", "122-665-333")
Dim strLookFor As String = InputBox("Type in the person you want to call", "Phone a Friend").ToUpper
If dic.ContainsKey(strLookFor) Then
MessageBox.Show(dic(strLookFor))
Else
MessageBox.Show("Match not found.")
End If
答案 1 :(得分:1)
你说你不想要答案,只有提示,所以这是我的:
您不需要循环,也不需要intCounter
或found
。
IndexOf
如果没有匹配则返回-1,如果匹配则返回数组元素的索引。
答案 2 :(得分:-1)
我今天早上使用搜索数组而不是array.indexof来运行它。抱歉,如果发布不正确,我在visual basic express上运行它。谢谢你的帮助。我做的修订是(供将来参考) strNames(intCounter).IndexOf(strLookFor)