显示有价值的名称而不是它的值

时间:2015-09-11 11:40:20

标签: vb.net variables

我正在建立学校委员会投票系统,用户输入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

2 个答案:

答案 0 :(得分:0)

为候选人和投票计数器使用数组:

Dim CandidateTotal(5) As Integer
Dim NumericUpDown(5)(5) As Integer

然后相应地修改您的程序。您可以忽略第一个数组元素(0)并使用1-5。修改程序使用CandidateTotal(n),其中n =候选者和NumericUpDown(n)(x),其中n =候选人,x =投票代替现有代码,然后当您找到获胜者时,您将拥有索引(1-5号)这是赢得的候选人。

答案 1 :(得分:0)

win变量应该在第一个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”变量是无效的,你可以删除它。

正如一些人所说,阵列是一个好主意。