在单个文本框中添加数字并使用方法将值返回到列表框

时间:2018-04-29 02:37:36

标签: vb.net

我正在尝试让用户在等级中输入等级,例如" 94 85 46"当他们点击提交按钮时,数字将从方法中获取。在方法内部,数字单独乘以百分比,并加在一起,最终值列在列表框中。

EX:" 100 85 93" =(100 * .15)+(85 * .15)+(93 * .15)= 41.7

我遇到的问题是我收到此错误 System.InvalidCastException:' from string""输入' Double'无效。' ,它指向num  它告诉我问题是。

quiz += space

我不明白我做错了什么。

我的代码是

Private Sub submitButton_Click(sender As Object, e As EventArgs) Handles submitButton.Click
    DisplayExamGrades() ' display grade for exams
    DisplayQuizGrades() ' display grade for quizzes
    DisplayAssignmentGrades() 'display grade for assignments
    DisplayAttendanceGrade() ' display grade for attendance
    DisplayFinalProjectGrade() 'display grade for final project


End Sub

'method for calculating quiz Grades
Sub DisplayQuizGrades()
    Dim a As String() = quizGradesTextBox.Text.Split(" "c)
    Dim quiz As Decimal
    For Each space As String In a
        quiz += space
        If quizGradesTextBox.Text.Length > 11 Then
            MessageBox.Show("Enter only 4 grades!")
        End If
        If quiz > 100 Then
            MessageBox.Show("Individual grade numbers must be less than 100!")
        End If
        quiz *= 0.15
    Next
    resultListBox.Items.Add(quiz)
End Sub

2 个答案:

答案 0 :(得分:0)

好的关于您的问题,计算是尝试将String类型添加到Decimal类型。这个字符串绝对可以包括字母和符号。 VB尝试使用Double类型进行所有计算。在这种情况下,错误是说尝试将字符串转换为double是无效的。所以,代码应该是

quiz += CDec(space)

我还注意到代码中有一些需要调整的位。我已经在下面添加了一些内联注释代码。希望它有所帮助

Sub DisplayQuizGrades()
    Dim a As String() = quizGradesTextBox.Text.Split(" "c)
    Dim quiz As Decimal
    'The  check for number of grades entered shouldn't be just checking the length of the string
    'If a users enters 4 grades of 100 (unlikely i know), the length of the string will be 15
    'What you should really do, is check how many elements there are in the split array and 
    'make sure that they are all valid numbers before any calculactions are done on them.
    'If there is an error, exit this sub without adding anything to the list
    If a.Count > 4 Then
        MessageBox.Show("Enter only 4 grades!")
        'this is the line that you exit the sub if there are more than 4 grades
        Exit Sub
    End If
    For Each space As String In a
        If (Val(space) > 100) Or Val(space) <= 0 Then
            MessageBox.Show("Individual grade numbers must be greater than 0 and less than 100!")
            'this is the line that you exit the sub if there are invalid grades
            Exit Sub
        End If
        'here is the line that was causing you problems
        ' I have also moved it so that the number would only be added after the validation check
        quiz += CDec(Val(space)*.15)
    Next
    ' and would end up with completely the wrong result :-)
    resultListBox.Items.Add(quiz)
End Sub

答案 1 :(得分:0)

我将等级数(2)除以百分比,以便最多百分比只能为15

Sub DisplayQuizGrades()
    'take out the spaces in the string
    Dim a As String() = quizTextBox.Text.Split(" "c)
    Dim quiz As Decimal ' holds grade values

    'limit grades entered 
    If a.Count > 4 Then
        MessageBox.Show("Only 2 grades or less!")
        Exit Sub
    End If
    For Each space As String In a
        If (Val(space) > 100) Or Val(space) < 0 Then ' limit grades to be 0 - 100
            MessageBox.Show("Individual grade numbers must be greater than 0 and less than 100!")
            Exit Sub
        End If

        quiz += CDec(space) ' add together numbers in textbox
    Next

    quiz = CDec(quiz * 0.0375) ' multiply added grades by percentage and divide by amount of numbers in text box
    resultListBox.Items.Add(quiz)
End Sub