程序逻辑使用visual basic 2010计算平均值

时间:2014-02-26 01:37:06

标签: vb.net visual-studio-2010

我正在尝试使用Visual Basic 2010计算平均值,并在消息窗口中显示此平均值。在调试(注释掉代码并尝试将不同的变量作为字符串发送到消息框)时,我得到了消息窗口,但没有返回数据。

由于我现在有代码,我没有收到任何错误,但我甚至无法显示消息框。

基本上,程序让用户输入他们已阅读的书籍数量,然后根据阅读的书籍数量计算总点数。该练习要求保持所有书籍的平均读数。

Public Class BookPoints

'Declare module level variables for summary information
Private PointInteger, BookAmountInteger, SummaryBookTotal, ReaderCount As Integer

Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
    'Exit the program
    Me.Close()
End Sub

Private Sub ClearToolStripMenuItem_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles ClearToolStripMenuItem.Click
    With NameTextBox2
        .Clear()
        .Focus()
        .Select()
    End With
    BooksTextBox1.Clear()
    PointsTextBox3.Clear()
End Sub

Private Function Points(ByVal BookAmountInteger As Integer) As Integer
    'Calculate point total
    If BookAmountInteger <= 3 Then
        Return BookAmountInteger * 10
    ElseIf BookAmountInteger >= 4 AndAlso BookAmountInteger <= 6 Then
        Return 30 + (BookAmountInteger - 3) * 15
    Else
        Return 75 + (BookAmountInteger - 6) * 20
    End If
End Function

Private Sub PointsToolStripMenuItem_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles PointsToolStripMenuItem.Click
    'calculate points and display total
    Try
        PointInteger = Integer.Parse(BooksTextBox1.Text)
        PointsTextBox3.Text = Points(PointInteger).ToString()
    Catch BookAmountException As FormatException
        MessageBox.Show("Amount of books read must numeric.", _
                        "Data Entry Error", _
                        MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        With BooksTextBox1
            .Clear()
            .Focus()
            .Select()
        End With

        'calculate average of books read
        Dim Total As Decimal
        Total = Decimal.Parse(BooksTextBox1.Text)

        If Total <> 0 Then
            Total += SummaryBookTotal
            ReaderCount += 1
        End If
    End Try
End Sub

Private Sub SummaryToolStripMenuItem_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles SummaryToolStripMenuItem.Click
    Dim AverageBooks As Decimal
    Dim MessageString As String

    If ReaderCount > 0 Then
        AverageBooks = SummaryBookTotal / ReaderCount

        MessageString = "Average books read: " &
            SummaryBookTotal.ToString()
        MessageBox.Show(MessageString, "Average of Books Read", _
                        MessageBoxButtons.OK, MessageBoxIcon.Information)
    End If
End Sub
End Class

0 个答案:

没有答案