我需要为学校创建一个程序。要求是:设计一个有五个按钮的程序,每个等级一个。每次按下按钮,它都会将该等级增加1。同时,它会更新一个标签,显示通过考试的当前学生百分比(成绩高于F)。 我们一直在使用计数器和if ... else语句/ case ... select语句。
这是我到目前为止所做的:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Static intCounterA As Integer = 0
Static intCounterB As Integer = 0
Static intCounterC As Integer = 0
Static intCounterD As Integer = 0
Static intCounterF As Integer = 0
Static intCounterTotal As Integer = 0
Dim dblTotal As Double
dblTotal = intCounterTotal
Me.lblTotal.Text = dblTotal
End Sub
Private Sub btnA_Click(sender As Object, e As EventArgs) Handles btnA.Click
Static intCounterA As Integer = 0
intCounterA = intCounterA + 1
Me.lblA.Text = intCounterA
End Sub
Private Sub btnB_Click(sender As Object, e As EventArgs) Handles btnB.Click
Static intCounterB As Integer = 0
intCounterB = intCounterB + 1
Me.lblB.Text = intCounterB
End Sub
Private Sub btnC_Click(sender As Object, e As EventArgs) Handles btnC.Click
Static intCounterC As Integer = 0
intCounterC = intCounterC + 1
Me.lblC.Text = intCounterC
End Sub
Private Sub btnD_Click(sender As Object, e As EventArgs) Handles btnD.Click
Static intCounterD As Integer = 0
intCounterD = intCounterD + 1
Me.lblD.Text = intCounterD
End Sub
Private Sub btnF_Click(sender As Object, e As EventArgs) Handles btnF.Click
Static intCounterF As Integer = 0
intCounterF = intCounterF + 1
Me.lblF.Text = intCounterF
End Sub
我只需要lblTotal来显示正在通过的学生的百分比,而我所拥有的只是返回NaN。有什么帮助吗?
答案 0 :(得分:1)
正如我在评论中所说,试试这样的事情。
我建议缩短变量名,因为它会使代码编写起来很乏味。
每当你开始需要多个变量互相交流时,使用集合或数组写起来会更容易,但我知道你还在学习。
在UpdateTotals
子句中,我添加了两个不是真正需要的变量,只是为了让代码更容易阅读。
出于某种原因,您在Form1_Load
范围内将变量声明为静态,这似乎是错误的。
如果你想以这种方式使用它们,请将它们设为全局。
此外,请勿在按钮点击事件中重新定义它们...
修改:代码已构建并正常运行:
Public Class Form1
Dim intCounterA As Integer = 0
Dim intCounterB As Integer = 0
Dim intCounterC As Integer = 0
Dim intCounterD As Integer = 0
Dim intCounterF As Integer = 0
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.lblA.Text = intCounterA
Me.lblB.Text = intCounterB
Me.lblC.Text = intCounterC
Me.lblD.Text = intCounterD
Me.lblF.Text = intCounterF
End Sub
Private Sub UpdateTotal()
Dim n, d
n = (intCounterA + intCounterB + intCounterC + intCounterD + intCounterF) - intCounterF
d = intCounterA + intCounterB + intCounterC + intCounterD + intCounterF
Me.lblTotal.Text = Math.Round(((n / d) * 100), 2) & "%" 'Display as percentage - round to 2 decimals
End Sub
Private Sub btnA_Click(sender As Object, e As EventArgs) Handles btnA.Click
intCounterA = intCounterA + 1
Me.lblA.Text = intCounterA
UpdateTotal()
End Sub
Private Sub btnB_Click(sender As Object, e As EventArgs) Handles btnB.Click
intCounterB = intCounterB + 1
Me.lblB.Text = intCounterB
UpdateTotal()
End Sub
Private Sub btnC_Click(sender As Object, e As EventArgs) Handles btnC.Click
intCounterC = intCounterC + 1
Me.lblC.Text = intCounterC
UpdateTotal()
End Sub
Private Sub btnD_Click(sender As Object, e As EventArgs) Handles btnD.Click
intCounterD = intCounterD + 1
Me.lblD.Text = intCounterD
UpdateTotal()
End Sub
Private Sub btnF_Click(sender As Object, e As EventArgs) Handles btnF.Click
intCounterF = intCounterF + 1
Me.lblF.Text = intCounterF
UpdateTotal()
End Sub
End Class