VB添加2个数字

时间:2015-11-17 12:45:26

标签: vb.net

这看起来很简单,但我很困惑。我对VB有很多经验,这不是垃圾邮件问题。

标签结果应为其先前值加上输入的新销售值。这段代码怎么不对?

Option Strict Off

Public Class Form1

Dim totalpointsaccumultator As Object

Private Sub exitButton_Click(sender As Object, e As EventArgs) Handles exitButton.Click
    Me.Close()
End Sub

Public Sub assignButton_Click(sender As Object, e As EventArgs) Handles assignButton.Click
    Dim inputProjectPoints, inputTestPoints As String
    Dim grade, projectpoints, testpoints As String
    Dim projectcounter As Integer = 1
    Dim testcounter As Integer = 1
    Dim isconverted As Boolean
    Dim totalpointsaccumulator As Integer
    Do While projectcounter < 5
        inputProjectPoints = InputBox("Enter the points earned on project " & projectcounter, "Grade Calculator", "0")
        inputProjectPoints = projectpoints
        isconverted = Integer.TryParse(inputProjectPoints, CInt(projectpoints))
        If isconverted Then
            totalpointsaccumultator = totalpointsaccumulator + projectpoints
            projectcounter = projectcounter + 1
        Else
            MessageBox.Show("Please enter a number.", "Grade Calculator", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    Loop

    Do While testcounter < 3
        inputTestPoints = InputBox("Enter the points earned on test " & testcounter, "Grade Calculator", "0")
        isconverted = Integer.TryParse(inputTestPoints, testpoints)
        If isconverted Then
            testcounter = testcounter + 1
            totalpointsaccumulator = CInt(totalpointsaccumulator + testpoints)
        Else
            MessageBox.Show("Please enter a number.", "Grade calculator", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    Loop

    ' assign grade
    Select Case totalpointsaccumulator
        Case Is >= 360
            grade = "A"
        Case Is >= 320
            grade = "B"
        Case Is >= 280
            grade = "C"
        Case Is >= 240
            grade = "D"
        Case Else
            grade = "F"
    End Select
    totalpointsLabel.Text = Convert.ToString(totalpointsaccumulator)
    gradeLabel.Text = grade
End Sub

结束班

编辑:我在修改Option Strict后对此进行了修改,现在可以使用,感谢所有想法和帮助。

选项明确开启

Public Class Form1

Private Sub exitButton_Click(sender As Object, e As EventArgs) Handles exitButton.Click
    Me.Close()
End Sub

Public Sub assignButton_Click(sender As Object, e As EventArgs) Handles assignButton.Click

    For counter = 1 To 4
        Dim grade = InputBox("Grade " & counter)
        Do Until IsNumeric(grade) = True
            MessageBox.Show("Please enter a number.", "Grade Calculator", MessageBoxButtons.OK, MessageBoxIcon.Information)
            grade = InputBox("Grade " & counter)
        Loop
        Dim totalgrade = 0
        totalgrade = totalgrade + grade
        Select Case totalgrade
            Case Is >= 360
                grade = "A"
            Case Is >= 320
                grade = "B"
            Case Is >= 280
                grade = "C"
            Case Is >= 240
                grade = "D"
            Case Else
                grade = "F"
        End Select
        totalpointsLabel.Text = totalgrade
        gradeLabel.Text = grade
    Next
End Sub

结束班

1 个答案:

答案 0 :(得分:2)

你的对象总数错误积累 t ator和totalpointsaccumulator ......

我不认为需要

Dim totalpointsaccumultator As Object
'                     ^ here´s your mistake

在你的职能之外

我认为您的代码应该是这样的:

Option Explicit On

Public Class Form1

' Dim totalpointsaccumultator As Object
' ^ this line removed 

Private Sub exitButton_Click(sender As Object, e As EventArgs) Handles exitButton.Click
    Me.Close()
End Sub

Public Sub assignButton_Click(sender As Object, e As EventArgs) Handles assignButton.Click
    Dim inputProjectPoints, inputTestPoints As String
    Dim grade, projectpoints, testpoints As String
    Dim projectcounter As Integer = 1
    Dim testcounter As Integer = 1
    Dim isconverted As Boolean
    Dim totalpointsaccumulator As Integer
    Do While projectcounter < 5
        inputProjectPoints = InputBox("Enter the points earned on project " & projectcounter, "Grade Calculator", "0")
        inputProjectPoints = projectpoints
        isconverted = Integer.TryParse(inputProjectPoints, CInt(projectpoints))
        If isconverted Then
            totalpointsaccumulator = totalpointsaccumulator + projectpoints
            '                 ^ "t" removed
            projectcounter = projectcounter + 1
            totalpointsLabel.Text = totalpointsaccumulator.ToString()
            ' new 
        Else
            MessageBox.Show("Please enter a number.", "Grade Calculator", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    Loop

    Do While testcounter < 3
        inputTestPoints = InputBox("Enter the points earned on test " & testcounter, "Grade Calculator", "0")
        isconverted = Integer.TryParse(inputTestPoints, testpoints)
        If isconverted Then
            testcounter = testcounter + 1
            totalpointsaccumulator = totalpointsaccumulator + testpoints
            '                        ^ cint( not needed
        Else
            MessageBox.Show("Please enter a number.", "Grade calculator", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    Loop

    ' assign grade
    Select Case totalpointsaccumulator
        Case Is >= 360
            grade = "A"
        Case Is >= 320
            grade = "B"
        Case Is >= 280
            grade = "C"
        Case Is >= 240
            grade = "D"
        Case Else
            grade = "F"
    End Select
    ' totalpointsLabel.Text = totalpointsaccumulator.ToString()
    gradeLabel.Text = grade
End Sub