我正在制作一个学校理事会投票系统,其中用户输入5名候选人的投票偏好。然后,程序将这些分数加起来,显示得分最低的候选人为获胜者。就是在这一点上我被卡住了。我的代码只显示得分最低的候选人的值,而不是该分数所属的名称。这是我目前为止写的内容:
Public Class EnterVotes
Dim winner As Integer = Candidate1Total
Private Sub Label2_Click(sender As Object, e As EventArgs) Handles Label2.Click
End Sub
Private Sub VScrollBar1_Scroll(sender As Object, e As ScrollEventArgs)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim int As Integer
int = NumericUpDownC1V1.Value
int = NumericUpDownC1V2.Value
int = NumericUpDownC1V3.Value
int = NumericUpDownC1V4.Value
int = NumericUpDownC1V5.Value
Dim Candidate1Total As Integer
Dim Candidate2Total As Integer
Dim Candidate3Total As Integer
Dim Candidate4Total As Integer
Dim Candidate5Total As Integer
Try
Candidate1Total = NumericUpDownC1V1.Value + (NumericUpDownC1V2.Value * 2) + (NumericUpDownC1V3.Value * 3) + (NumericUpDownC1V4.Value * 4) + (NumericUpDownC1V5.Value * 5)
Candidate2Total = NumericUpDownC2V1.Value + (NumericUpDownC2V2.Value * 2) + (NumericUpDownC2V3.Value * 3) + (NumericUpDownC2V4.Value * 4) + (NumericUpDownC2V5.Value * 5)
Candidate3Total = NumericUpDownC3V1.Value + (NumericUpDownC3V2.Value * 2) + (NumericUpDownC3V3.Value * 3) + (NumericUpDownC3V4.Value * 4) + (NumericUpDownC3V5.Value * 5)
Candidate4Total = NumericUpDownC4V1.Value + (NumericUpDownC4V2.Value * 2) + (NumericUpDownC4V3.Value * 3) + (NumericUpDownC4V4.Value * 4) + (NumericUpDownC4V5.Value * 5)
Candidate5Total = NumericUpDownC5V1.Value + (NumericUpDownC5V2.Value * 2) + (NumericUpDownC5V3.Value * 3) + (NumericUpDownC5V4.Value * 4) + (NumericUpDownC5V5.Value * 5)
Label7.Text = Candidate1Total
Label8.Text = Candidate2Total
Label9.Text = Candidate3Total
Label10.Text = Candidate4Total
Label11.Text = Candidate5Total
Catch
End Try
If Candidate2Total < winner Then
winner = Candidate2Total
End If
If Candidate3Total < winner Then
winner = Candidate3Total
End If
If Candidate4Total < winner Then
winner = Candidate4Total
End If
If Candidate5Total < winner Then
winner = Candidate5Total
End If
Label3.Text = winner
为候选人和投票计数器使用数组:
Dim CandidateTotal(5) As Integer
Dim NumericUpDown(5)(5) As Integer
然后相应地修改你的程序。你可以忽略第一个数组元素(0),使用1-5。修改程序,使用CandidateTotal(n) (n =candidate)和NumericUpDown(n)(x) (n =candidate和x=vote)代替现有代码,然后当您找到获胜者时,您将获得获胜候选人的索引(编号1-5)。
应该将winner变量设置在第一个if语句之前。我不明白你的代码是怎么编译的。
您需要存储两个变量,一个用于分数,另一个用于名称。
Dim winnerScore As Integer = Candidate1Total
Dim winnerName As String = "Name of candidate 1"
If Candidate2Total < winnerScore Then
winnerScore = Candidate2Total
winnerName As String = "Name of candidate 2"
End If
If Candidate3Total < winnerScore Then
winnerScore = Candidate3Total
winnerName As String = "Name of candidate 3"
End If
If Candidate4Total < winnerScore Then
winnerScore = Candidate4Total
winnerName As String = "Name of candidate 4"
End If
If Candidate5Total < winnerScore Then
winnerScore = Candidate5Total
winnerName As String = "Name of candidate 5"
End If
Label3.Text = winnerScore
' Display winnerName somewhere
在您的代码中,您将正确设置winnerName。我也不知道你想把名字显示在哪里。
你的"int"变量是没有意义的,你可以删除它。
如前所述,数组是个好主意。