计算Visual Basic中成绩合格与否的学生人数

时间:2018-12-14 21:03:22

标签: vb.net

我正在尝试创建一个应用程序,该应用程序允许用户依次输入一个字母等级(大写或小写),并连续显示通过(D或更好)的学生人数和失败的人数。每次输入成绩并单击按钮时,它都应计为不及格或不及格。由于某种原因,我的数为1,然后在我输入另一个时重置。我怎样才能解决这个问题?我一直在寻找问题的答案,但是我没有运气。

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'declare variables
        Dim grade As String = 0
        Dim passNumber As Integer
        Dim failNumber As Integer
        'calculate
        grade = Me.txtGrade.Text
        If grade = "D" Or grade = "C" Or grade = "A" Or grade = "B" Or grade = "d" Or grade = "c" Or grade = "a" Or grade = "b" Then passNumber = passNumber + 1
        If grade = "F" Or grade = "f" Then failNumber = failNumber + 1
        'display message
        Me.lblFail.Text = failNumber
        Me.lblPass.Text = passNumber
    End Sub

3 个答案:

答案 0 :(得分:1)

您的passNumber和failNumber变量是函数调用的局部变量。将它们移到方法之外,以免在每次调用该方法时都将其重置:

Private passNumber As Integer
Private failNumber As Integer

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    'declare variables
    Dim grade As String = 0

    'calculate
    grade = Me.txtGrade.Text
    If grade = "D" Or grade = "C" Or grade = "A" Or grade = "B" Or grade = "d" Or grade = "c" Or grade = "a" Or grade = "b" Then passNumber = passNumber + 1
    If grade = "F" Or grade = "f" Then failNumber = failNumber + 1
    'display message
    Me.lblFail.Text = failNumber
    Me.lblPass.Text = passNumber
End Sub

答案 1 :(得分:1)

正如Meta已经指出的那样,问题在于passNumber和failNumber变量的本地化。我建议也做这样的事情,以使代码更具可读性和可维护性。

Dim passNumber As Integer
Dim failNumber As Integer

Dim PassArray As String() = {"a", "b", "c", "d"}
Dim FailArray As String() = {"f"}

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    'declare variables
    Dim grade As String = Me.txtGrade.Text.ToLower()

    If PassArray.Contains(grade) Then
        passNumber += 1
    ElseIf FailArray.Contains(grade) Then
        failNumber += 1
    Else
        MsgBox("Error: Invalid Grade")
    End If

    'display message
    Me.lblFail.Text = failNumber
    Me.lblPass.Text = passNumber
End Sub

答案 2 :(得分:1)

在线发表评论和解释。 编辑感谢@Blackwood的评论。我已将方法外部的passNumber和failNumber移到了Form级别(类级别)。这样,他们将在事件调用之间保留那里的值。

Private passNumber As Integer
Private failNumber As Integer

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    'Using .ToUpper as suggested in Comments by David Wilson
    Dim grade = txtGrade.Text.ToUpper()
    'I used a Select Case because I think it is 
    'easier to read.
    Select Case grade
        Case "A", "B", "C", "D"
            'the += 1 is just a shortcut for
            'writing = passNumber + 1
            passNumber += 1
        Case "F"
            failNumber += 1
        Case Else
            MessageBox.Show("Invalid grade")
    End Select
    'You need to convert your Integers to Strings
    'so they can go into the Text property of your labels.
    lblFail.Text = CStr(failNumber)
    lblPass.Text = CStr(passNumber)
End Sub