1

我有一个 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
4

3 回答 3

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
于 2012-11-26T06:30:06.787 回答
1

你说你不想要答案,只想要提示,所以这是我的:

您不需要循环,也不需要intCounteror found

IndexOf如果不匹配则返回 -1,如果匹配则返回数组元素的索引。

于 2012-11-26T04:06:06.473 回答
-1

我今天早上使用搜索数组而不是 array.indexof 让它工作。抱歉,如果发布不正确,我正在 Visual Basic Express 上运行它。感谢人们的帮助。我所做的修订是(供将来参考)strNames (intCounter).IndexOf (strLookFor)

于 2012-11-26T11:38:47.270 回答