2015-07-12 55 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

相關問題