如何在visual basic中存储数字列表?我正在研究一个项目,需要显示符合最大身高的患者列表。以下是我不完整的代码。我找到了最大高度,但是我怎么能以某种方式存储与最大高度相匹配的高度指数来显示它们?感谢
Public Class Form4
Dim MaxHeight As Integer = 0
Dim x As Integer = 0
Dim y As Integer = 0
Dim MaxHeights() As Integer
Dim MaxHeightCount As Integer = 0
Private Sub btnFindMaxHeight_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFindMaxHeight.Click
While x < PatientCount
If Heights(x) > MaxHeight Then
MaxHeight = Heights(x)
End If
x = x + 1
End While
ReDim MaxHeights(x)
x = 0
While y < PatientCount
If Heights(y) = MaxHeight Then
MaxHeights(x) = y 'If the patient with the index "i" has the maxheight, add their index "i" to the array.
x = x + 1
MaxHeightCount = MaxHeightCount + 1
End If
End While
x = 0
While x < MaxHeightCount
ListBox1.Items.Add(Names(MaxHeights(x)))
x = x + 1
End While
End Sub
End Class
答案 0 :(得分:0)
检查一下:
Public Class Form4
Dim MaxHeight As Integer = 0
Dim x As Integer = 0
Dim y As Integer = 0
Dim MaxHeights() As Integer
Private Sub btnFindMaxHeight_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFindMaxHeight.Click
for x=0 to Maxheights.count-1
if(maxheghts(x)=maxheight)
messagebox.show(x)
end if
next
End Sub
End Class
答案 1 :(得分:0)
如果您想要做的是获得一组最高的患者指数,您可以尝试这样的事情:
Public Class Form4
Dim MaxHeight As Integer = 0
Dim MaxHeights() As Integer
Private Sub btnFindMaxHeight_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFindMaxHeight.Click
Dim x as integer = 0;
For i as integer = 0 to PatientCount - 1
If Heights(i) > MaxHeight Then
MaxHeight = Heights(i)
x += 1
End If
Next
ReDim MaxHeights(x)
x = 0
For i as Integer = 0 to PatientCount - 1
If Heights(i) = MaxHeight Then
MaxHeights(x) = i 'If the patient with the index "i" has the maxheight, add their index "i" to the array.
x += 1
End If
Next
End Sub
End Class
这应该存储MaxHeight
数组中MaxHeights()
的所有患者的指数。然后,您可以使用循环和MaxHeights
中的数据(假设您有一个名为listbox1
的列表框)轻松访问每个患者的数据:
listbox1.Items.Add("Tallest Patients")
For i as Integer = 0 to MaxHeights.Length - 1
listbox1.Items.Add(Patients(MaxHeights(i)).Name)
Next
HTH。如果有什么不清楚,请告诉我。