我有5个值,我正在比较它们并尝试根据哪个值最大来做一些动作。
值为v1,v2,v3,v4和v5
If (v1 > v2 And v1 > v3 And v1 > v4 And v1 > v5) Then
l1st.Text = "Al-Haj Shaji Gul"
Label5.ForeColor = Color.Red
Me.PictureBox12.Image = Global.shaji.My.Resources.Resources.shajismal
ElseIf (v2 > v3 And v2 > v4 And v2 > v1 And v2 > v5) Then
l1st.Text = "Nor huq"
Label6.ForeColor = Color.Red
Me.PictureBox12.Image = Global.shaji.My.Resources.Resources.norlhuq
ElseIf (v3 > v1 And v3 > v2 And v2 > v4 And v3 > v5) Then
l1st.Text = "Darya Khan"
Label7.ForeColor = Color.Red
Me.PictureBox12.Image = Global.shaji.My.Resources.Resources.daryakhanpic
ElseIf (v4 > v1 And v4 > v2 And v4 > v3 And v4 > v5) Then
l1st.Text = "Imran Khan"
Label8.ForeColor = Color.Red
Me.PictureBox12.Image = Global.shaji.My.Resources.Resources.Imran_Khan
ElseIf (v5 > v1 And v5 > v2 And v5 > v3 And v5 > v4) Then
l1st.Text = "Zarnoor Afridi"
Label10.ForeColor = Color.Red
Me.PictureBox12.Image = Global.shaji.My.Resources.Resources.zarnorsmal
End If
答案 0 :(得分:3)
只是一个例子:
Dim v_array(4) As Integer
Dim max_v As Integer
For i = 0 To 4
v_array(i) = i + (Math.Rnd(15) * 10)
Next i
For Each v In v_array
If v > max_v Then
max_v = v
End If
Next v
Select Case max_v
Case v_array(0)
MsgBox "Something"
Case v_array(1)
MsgBox "Something 1"
Case v_array(2)
MsgBox "Something 2"
Case v_array(3)
MsgBox "Something 3"
Case v_array(4)
MsgBox "Something 4"
End Select
答案 1 :(得分:2)
看看这是否有帮助。它获得最高分并将其存储在max
中。然后它检查所有五个值,并且每个值与max
相同,即最高值,它将控件设置为该学生的值。如果有多个分数最高的分数,则会将这些学生添加到文本框文本的末尾,例如“Student1 = Student3”。我唯一无法理解的是图像将是最后得分最高的学生。
Dim max As Integer = {v1, v2, v3, v4, v5}.Max
Dim multiple As Boolean = False
l1st.Text = ""
If (v1 = max) Then
l1st.Text = "Al-Haj Shaji Gul"
Label5.ForeColor = Color.Red
Me.PictureBox12.Image = Global.shaji.My.Resources.Resources.shajismal
multiple = True
EndIf
If (v2 = max) Then
If multiple = True Then
l1st.Text = l1st.Text + " = "
EndIf
multiple = True
l1st.Text = l1st.Text + "Nor huq"
Label6.ForeColor = Color.Red
Me.PictureBox12.Image = Global.shaji.My.Resources.Resources.norlhuq
EndIf
If (v3 = max) Then
If multiple = True Then
l1st.Text = l1st.Text + " = "
EndIf
multiple = True
l1st.Text = l1st.Text + "Darya Khan"
Label7.ForeColor = Color.Red
Me.PictureBox12.Image = Global.shaji.My.Resources.Resources.daryakhanpic
EndIf
If (v4 = max) Then
If multiple = True Then
l1st.Text = l1st.Text + " = "
EndIf
multiple = True
l1st.Text = l1st.Text + "Imran Khan"
Label8.ForeColor = Color.Red
Me.PictureBox12.Image = Global.shaji.My.Resources.Resources.Imran_Khan
EndIf
If (v5 = max) Then
If multiple = True Then
l1st.Text = l1st.Text + " = "
EndIf
multiple = True
l1st.Text = l1st.Text + "Zarnoor Afridi"
Label10.ForeColor = Color.Red
Me.PictureBox12.Image = Global.shaji.My.Resources.Resources.zarnorsmal
End If
答案 2 :(得分:1)
首先将变量放入数组中;这使搜索最容易
Dim v = New () {v1, v2, v3, v4, v5}
Dim i As Integer = Array.IndexOf(v, v.Max()) + 1
Select Case i
Case 1
' Process when v1 is maximum
Case 2
' Process when v2 is maximum
Case 3
' Process when v3 is maximum
Case 4
' Process when v4 is maximum
Case 5
' Process when v5 is maximum
End Select
原因你也可以将一个变量v
定义为数组,而不是使用单个变量。
Dim v = New Integer(4) {}
v(0) = 10
v(1) = 15
...