2015-07-12 47 views
0

早上好,不能调和票与候选人

我正在投票程序在Visual Basic中(使用Visual Studio 2013,如果这有什么区别),我似乎无法得到每个候选人的投票都有效。 这是一个分配,所以我必须使用listbox.DoubleClick事件处理程序为选定的索引来计票。

到目前为止,我已经写出了我喜欢的整个程序,但我无法得到与候选人相符的选票。我认为这与代码

Try 
     'Selected candidate gets a vote tallied 
     vote(candidateList.SelectedIndex) += 1 
     candidateList.SelectedIndex = -1 
    Catch exc As IndexOutOfRangeException 
     MessageBox.Show("Please click on a candidate to vote.", "Attention!") 
    End Try 

我的整个项目这一块做的,如果需要可以在这里看到 - Voting Machine source code

任何想法会帮助我的程序协调的数据?或者我的错误是什么? 谢谢!

编辑 - 我现在主要工作,我得到每个候选人的准确票数,但现在我需要弄清楚如何在同一个列表框中获得候选人名称和他们的总票数。 上面的代码以正确的顺序显示正确的投票,但是如果我尝试将candidateList.selectedItem添加到混合中,它将引发无效的转换异常,因为字符串(候选名称)无法转换为整数。我如何获得selectedItem并增加所选索引的计数?现在,我在今天的大部分时间都停留在这一点上,我将不胜感激。 谢谢!

+0

我已经注释掉的StreamReader和StreamWriter线为未使用它们。他们从此前的尝试中获得这一成果。 –

+0

使用下面的方法修复了错误之后,我现在得到doubleclick的输出,但是它是数字的,而不是ata与投票有关。 也就是说...如果我添加三个候选人并为其中的两个投票一次,第三个投票两次,它将在换行符上输出0 - 5。在这一点上我完全困惑。 –

回答

0

我想出解决我的问题。非常感谢那些帮助我的人!

Imports System.IO 

公共类别表决

Dim Candidates() As String 
Dim votes() As Integer 


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 

    'Get default values and views for Voting Machine 
    resultsList.Visible = False 
    candidateList.Location = New Point(143, 71) 
    tallyVotes.Enabled = False 
    resultsList.Enabled = False 
    Label1.Text = "Click 'Nominate Candidate' to enter a candidate, or 'Start Voting'" & vbCrLf & "to end nominations and start the voting." 

End Sub 

Private Sub candidateList_SelectedIndexChanged(sender As Object, e As EventArgs) Handles candidateList.SelectedIndexChanged 

End Sub 

Private Sub resultsList_SelectedIndexChanged(sender As Object, e As EventArgs) Handles resultsList.SelectedIndexChanged 

End Sub 

Private Sub nominateCandidate_Click(sender As Object, e As EventArgs) Handles nominateCandidate.Click 

    'Instructions 
    MessageBox.Show("When finished entering candidates, simply press enter on a blank line.", "Instructions") 

    'Gather list of Candidates 
    Dim candidateName = InputBox("Enter the name of your candidate (first and last name please):", "Candidate Name", "", 500, 500) 
    Dim i As Integer = 0 
    'Loops until a null string is entered signaling the end of input 
    Do Until String.IsNullOrEmpty(candidateName) 
     ReDim Preserve Candidates(i) 
     Candidates(i) = candidateName 
     i += 1 
     candidateName = InputBox("Enter the name of your candidate (first and last name please):", "Candidate Name", "", 500, 500) 
    Loop 

End Sub 

Private Sub startVoting_Click(sender As Object, e As EventArgs) Handles startVoting.Click 

    'Disable the Nomination button 
    nominateCandidate.Enabled = False 

    'Enable the tally votes button 
    tallyVotes.Enabled = True 

    'Set the label text to give instructions for tallying votes. 
    Label1.Text = "Vote for a candidate by double-clicking his or her name." & vbCrLf & "End the voting by clicking on 'Tally Votes'." 

    'Call sub to display the candidates for voting 
    showCandidates() 

End Sub 

Private Sub tallyVotes_Click(sender As Object, e As EventArgs) Handles tallyVotes.Click 

    'Makes results listbox visible and moves the candidate list to the left 
    resultsList.Visible = True 
    candidateList.Location = New Point(14, 71) 
    'Call the sub to tally the votes and display the winner 
    getResults() 

End Sub 

Private Sub candidateList_DoubleClick(sender As Object, e As EventArgs) Handles candidateList.DoubleClick 

    'Selected candidate gets a vote tallied 
    Try 
     'Selected candidate gets a vote tallied 
     votes(candidateList.SelectedIndex) += 1 
     candidateList.SelectedIndex = -1 
    Catch exc As IndexOutOfRangeException 
     MessageBox.Show("Please click on a candidate to vote.", "Attention!") 
    End Try 

End Sub 

Sub showCandidates() 

    'Display the candidates in the listbox and sort alphabetically by last name 
    Dim query = From candidate In Candidates 
       Let firstName = candidate.Split(" "c)(0) 
       Let lastName = candidate.Split(" "c)(1) 
       Let name = firstName & " " & lastName 
       Order By lastName 
       Select name 

    For Each Name As String In query 
     candidateList.Items.Add(Name) 
    Next 

    ReDim Preserve votes(candidateList.Items.Count - 1) 

End Sub 

Sub getResults() 

    'Add the results to the Results Listbox 
    For Each i In votes 
     resultsList.Items.Add(i) 
    Next 

    'Declare Winner 
    Dim mostVotes As Integer = 0 
    For Each item In resultsList.Items 
     If item > mostVotes Then 
      mostVotes = item 
     End If 
    Next 

    resultsList.SelectedItem = mostVotes 
    candidateList.SelectedIndex = resultsList.SelectedIndex 

    Dim winner = candidateList.SelectedItem 

    MessageBox.Show("The winner is " & winner) 

End Sub 

末级

0

我看了看你的代码。在开始使用之前,您需要初始化“投票”数组。你有这条线:

Dim vote() As Integer 

这实际上并没有初始化整数数组。以下是一些可以初始化的例子,其长度为3或3个变量。

Dim vote(3) As Integer 
Dim vote As Integer() = {1,2,3} 

顺便说一下,最好还是检查“candidateList.SelectedIndex”是否有实际的有效值(不是-1)。

+0

这里的问题是,我不知道数组有多大。候选人的数量取决于用户输入的数量,因此初始化投票数组会与候选人数量相冲突吗? –

+0

可能最好使用其他数据结构,例如List(Of Integer)。 – Saragis

+0

谢谢你,你的回答和satraj都帮助阻止我的错误,但我仍然坚持让选票与选定的索引保持一致。我觉得那条抛出错误的线是罪魁祸首,但不知道如何解决它。 –

0

在你的代码,投票数组声明但尚未初始化这是造成的NullReferenceException

请初始化适当维数组,比如你可以做的是,在你的代码“showCandidates()”功能下面,

Sub showCandidates() 


    'Display the candidates in the listbox and sort alphabetically by last name 
    Dim query = From candidate In Candidates 
       Let firstName = candidate.Split(" "c)(0) 
       Let lastName = candidate.Split(" "c)(1) 
       Let name = firstName & " " & lastName 
       Order By lastName 
       Select name 

    For Each Name As String In query 
     candidateList.Items.Add(Name) 
    Next 

'Initialize the vote array. 
ReDim Preserve vote(candidateList.Items.Count) 


End Sub 

使用ReDim投票阵列每当你要添加的候选列表框(即提名的候选人),否则你可能会得到一个IndexOutOfBounds异常。

+0

List(整数)对于性能而言可能会更好,而不是使用ReDim。 – Saragis

相关问题