如何计算大于数字的整数,然后搜索数组以将数字与名称相关联

时间:2014-11-30 23:23:12

标签: arrays visual-studio-2010 for-loop

我有两个数组(_intCholesterol和_strPatientNames)。我想要完成的是查看一个数组并计算数组中大于200的整数,我已经在下面做了。但另外我需要搜索另一个数组(_strPatientName)并将发现的数字大于200与名称相关联。如Bob 272.然后将名称和高位写入文件。如何完成搜索和关联?

    Dim intCount As Integer = 0
    Dim objWriter As New IO.StreamWriter("e:/consult.txt")

    For Each intCholesterolLevel In _intCholesterolLevel
        If intCholesterolLevel > 200 Then
            intCount += 1
        End If
    Next

    lblOutliers.Visible = True
    lblOutliers.Text = "There were " & intCount & " people with levels above 200"

我使用的代码最终成为:

  Dim objWriter As New IO.StreamWriter("E:\consult.txt")

    ' See if file exists.
    If IO.File.Exists("E:\consult.txt") Then
        ' Run loop for numbers over 200 and write the file.
        For intCholesterolIndex = 0 To (_intCholesterolLevel.Length - 1)
            If _intCholesterolLevel(intCholesterolIndex) > 200 Then
                objWriter.WriteLine(_strPatientName(intCholesterolIndex))
                objWriter.WriteLine(_intCholesterolLevel(intCholesterolIndex))
            End If
        Next
    Else
        MsgBox("The file is not available, try again")
    End If

    objWriter.Close()

1 个答案:

答案 0 :(得分:0)

我认为您可能正在寻找的是Dictionary而不是2个数组。

可能的示例

Dim cholesterolLevels As New Dictionary(Of String, Integer)

cholesterolLevels.Add("Bob", 272)
cholesterolLevels.Add("John", 190)
cholesterolLevels.Add("Joe", 205)
cholesterolLevels.Add("Bill", 165)

For Each patient As KeyValuePair(Of String, Integer) In cholesterolLevels
     Dim name As String = patient.Key 
     Dim level As Integer = patient.Value 

    If level > 200 Then
        intCount += 1
        objWriter.WriteLine(name & " - " & level)
    End if
Next

objWriter.Flush()
objWriter.Close() 

If intCount > 0
    lblOutliers.Visible = True
    lblOutliers.Text = "There were " & intCount & " people with levels above 200"
End if