1

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

3 回答 3

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
于 2013-03-28T19:34:31.943 回答
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
于 2013-03-28T19:36:44.497 回答
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
...
于 2013-03-28T20:04:10.633 回答