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ます。次に、5 つすべての値をチェックし、 と同じ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もちろん、個々の変数を使用する代わりに、最初から1 つの変数を配列として定義することもできます。

Dim v = New Integer(4) {}
v(0) = 10
v(1) = 15
...
于 2013-03-28T20:04:10.633 に答える