首页 > 解决方案 > 在 Visual Studio 的 2019 (VB) 中创建一个成绩计算器并在代码中挣扎一下

问题描述

我在 Visual Studio 2019 中使用 VB.Net 来帮助积累考试成绩数据。

我有一个名为 , 的标签和用于,的Score文本框,它们都是只读的。我也按和分数,以及。Score TotalScore CountScore AverageAddClearExit

我已经为 Clear Scores 和 Exit 按钮完成了代码,但我正在努力使用 Add 按钮并获取所有分数的输入和总和。

我的目标是显示方框中所有分数的总和、Score Total方框中的分数数量Score Count以及它们在方框中的平均值Score Average

这是我到目前为止所拥有的:

Public Class GradeCalculator
    Dim score As Integer
    Dim ScoreTotal As Decimal
    Dim ScoreCount As Decimal
    Dim Average As Integer

    Private Sub frmClearScores_Click(sender As Object, e As EventArgs) Handles frmClearScores.Click
        score = 0
        ScoreTotal = 0
        ScoreCount = 0
        Average = 0

        txtscore.Text = ""
        txtscoretotal.Text = ""
        txtscorecount.Text = ""
        txtaverage.Text = ""

        txtscore.Select()
    End Sub

    ' This is the "Add" button
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

    End Sub

    Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub
End Class

我怎样才能完成这个?

标签: vb.netvisual-studio

解决方案


Public Class GradeCalculator
    Dim ScoreTotal As Integer = 0
    Dim ScoreCount As Integer = 0

    Private Sub frmClearScores_Click(sender As Object, e As EventArgs) Handles frmClearScores.Click
        ScoreTotal = 0
        ScoreCount = 0

        txtscore.Text = ""
        txtscoretotal.Text = ""
        txtscorecount.Text = ""
        txtaverage.Text = ""

        txtscore.Select()
    End Sub

    ' This is the "Add" button
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        ScoreTotal += CInt(txtscore.Text)
        ScoreCount += 1

        txtscore.Text = ""
        txtscoretotal.Text = ScoreTotal.ToString()
        txtscorecount.Text = ScoreCount.ToString()
        txtaverage.Text = (CDec(ScoreTotal)/ScoreCount).ToString()

        txtscore.Select()
    End Sub

    Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub
End Class

推荐阅读