0

我正在使用 Visual Basic 开发一个程序,该程序结合了对选票类型程序使用顺序访问文件。用户单击保存投票按钮的每种类型,都会存储投票数量。我要做的是在我的访问文件中将投票数显示为实际数字。我现在的程序编写方式,候选人的名字出现的次数与保存的选票一样多。例如,如果 perez 被投票 4 次,则在访问文件中 perez 显示在 4 个不同的行上。我如何显示他们被投票的实际次数。我正在考虑使用计数器变量,但不确定如何真正实现它。这就是我到目前为止所拥有的。

Public Class Voter

Dim file As IO.File

Dim infile As IO.StreamReader

Dim outfile As IO.StreamWriter



Private Sub Voter_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    outfile = IO.File.CreateText("Votes.txt")
End Sub

Private Sub btnVote_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnVote.Click
    lstResult.Items.Clear()
    'which buttons is clicked
    If radMark.Checked = True Then
        outfile.WriteLine(radMark.Text)
        radMark.Checked = False
    ElseIf radSheima.Checked = True Then
        outfile.WriteLine(radSheima.Text)
        radSheima.Checked = False
    ElseIf radSam.Checked = True Then
        outfile.WriteLine(radSam.Text)
        radSam.Checked = False
    Else
        MessageBox.Show("You should select one among them")
    End If
End Sub

Private Sub btnResult_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnResult.Click
    'Dim Mark, Sheima, Sam As Integer
    Dim Mark As Integer = 0
    Dim Sheima As Integer = 0
    Dim Sam As Integer = 0
    Dim name As String
    'Mark = Sheima = Sam = 0
    outfile.Close()
    infile = IO.File.OpenText("Votes.txt")
    'keep track of votes
    While Not infile.EndOfStream
        name = infile.ReadLine()
        If name.Equals("Mark Stone") Then
            Mark += 1
        ElseIf name.Equals("Sheima Patel") Then
            Sheima += 1
        Else
            Sam += 1
        End If
    End While
    'results
    lstResult.Items.Clear()
    lstResult.Items.Add("Mark Stone     " & CStr(Mark))
    lstResult.Items.Add("Shemia Patel   " & CStr(Sheima))
    lstResult.Items.Add("Sam Perez      " & CStr(Sam))
    infile.Close()
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    Me.Close()

End Sub
End Class
4

1 回答 1

2

在您的 btnVote_Click 函数中,您每次单击时都会将单选按钮文本写入文件,这就是创建问题的方式。

此外,您正在计算名称在文件中出现的次数作为投票数,而不是数字。

您应该尝试将名称和计数放入您的投票文件中,而不仅仅是名称,例如

马克,1
希玛,2
相同,5

然后,当您单击投票按钮时,您应该读取 Mark 的数字,加 1 并将其写回。

试试这个,

Private Sub btnVote_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnVote.Click
    lstResult.Items.Clear()
    'which buttons is clicked
    If radMark.Checked = True Then
        AddCount(radMark.Text)
        radMark.Checked = False
    ElseIf radSheima.Checked = True Then
        AddCount(radSheima.Text)
        radSheima.Checked = False
    ElseIf radSam.Checked = True Then
        AddCount(radSam.Text)
        radSam.Checked = False
    Else
        MessageBox.Show("You should select one among them")
    End If
End Sub

Private Sub btnResult_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnResult.Click

    'results
    lstResult.Items.Clear()
    lstResult.Items.Add("Mark Stone     " & GetCount(radMark.Text))
    lstResult.Items.Add("Shemia Patel   " & CStr(radSheima.Text))
    lstResult.Items.Add("Sam Perez      " & CStr(radSam.Text))

End Sub

Private Sub AddCount(Byval VoteName As String)

    infile = New IO.File.StreamReader("Votes.txt")

    Dim whole_line As String
    Dim person As String
    Dim vote_count As Integer
    Dim found as boolean

    'read through the whole file until the end or find the name
    Do While infile.Peek() >= 0 And person <> VoteName

        'read each line
        whole_line = infile.ReadLine

        person = Split(whole_line, ",")(0)

        If person = VoteName Then

            vote_count = Split(whole_line, ",")(1)


            found = True
        End If
    Loop

    'Close the file after it is used.
    infile.Close()


    'Reopen the file with the StreamWriter
    outfile = IO.File.OpenText("Votes.txt")

    If found = True Then
        'the line will only be replace if the person name is found in the line
        whole_line = Whole_line.replace(VoteName & "," & vote_count, VoteName & "," & vote_count + 1)

        'Write back into the file
        outfile.WriteLine(whole_line)
    Else
        'if the vote name is not found in the file
        'Then create a new one
        outfile.WriteLine(VoteName & ",1" & VbCrLf)
    End If

        outfile.Close()

End Sub

Private Function GetCount(Byval VoteName As String)
    infile = New IO.File.StreamReader("Votes.txt")      

    Dim whole_line As String
    Dim person As String
    Dim vote_count As Integer

    'read through the whole file
    Do While infile.Peek() >= 0 And person <> VoteName

        'read each line
        whole_line = infile.ReadLine

        person = Split(Whole_line, ",")(0)

        If person = VoteName Then
            vote_count = Split(whole_line, ",")(1)
        End If
    Loop

    infile.Close()
    Return vote_count
End Sub
于 2012-04-27T00:57:59.213 回答