Public Class Form1
Dim a As Integer
Dim b As Integer
Dim c As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If CheckBox1.Checked = True Then
a += 1
Else : a += 0
End If
If CheckBox2.Checked = True Then
b += 1
Else : b += 0
End If
If CheckBox3.Checked = True Then
c += 1
Else : c += 0
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim max As Integer = 0
Dim d() As Integer = {a, b, c}
Dim f() As String = {"ch1", "ch2", "ch3"}
For i As Integer = 0 To 2
If max < d(i) Then
max = d(i)
Else : max = max
End If
Next
Label1.Text = f(max)
End Sub
End Class
答案 0 :(得分:0)
a,b和c的值在您单击第一个按钮时会递增,具体取决于其检查状态。然后当你循环并获得变量的最大值时,你试图将其用作f()数组中的indec。 f()数组只能从0到2,但max可以是任何值。您无法使用max从阵列中进行选择。你想做什么?
答案 1 :(得分:0)
因此,总而言之,每次点击Button1
时,您都会使用复选框来增加数字。这些值存储在变量a
,b
和c
中,当单击Button2
时,它们将被放入整数数组中。从代码的外观开始,您将尝试查找其中哪一个是最大值,并根据该索引显示另一个数组中的值。当然,问题在于您正在使用d
的值并将其用作f
的索引。然后,max
的值变为任意大的数字,很容易超出f
的范围,在您的示例中,Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim max As Integer = 0
Dim index as Integer = 0
Dim d() As Integer = {a, b, c}
Dim f() As String = {"ch1", "ch2", "ch3"}
For i As Integer = 0 To 2
If max < d(i) Then
max = d(i)
index = i
Else : max = max
End If
Next
Label1.Text = f(index)
End Sub
只有三个值。你可能想要更像这样:
max
这仍然使用For
作为比较器,但是当f
循环达到新的最大数字时会记录它的索引,使用它来提取index
的值,确保i
的值永远不会高于{{1}}。