Vb.net 使用方法将单个文本框中的数字相加并将值返回listbox

Vb.net 使用方法将单个文本框中的数字相加并将值返回listbox,vb.net,Vb.net,我试图让用户在“94 85 46”这样的文本框中输入分数,当他们单击提交按钮时,数字来自一个方法。在该方法中,数字分别乘以百分比,然后相加,最后的值列在列表框中 例如:“100 85 93”=(100*.15)+(85*.15)+(93*.15)=41.7 我遇到的问题是,我遇到了以下错误:System.InvalidCastException:“从字符串”到类型“Double”的转换无效。”并且它指向num 它告诉我问题是 quiz += space 我不明白我做错了什么 我的代码是 Pri

我试图让用户在“94 85 46”这样的文本框中输入分数,当他们单击提交按钮时,数字来自一个方法。在该方法中,数字分别乘以百分比,然后相加,最后的值列在列表框中

例如:“100 85 93”=(100*.15)+(85*.15)+(93*.15)=41.7

我遇到的问题是,我遇到了以下错误:System.InvalidCastException:“从字符串”到类型“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

好的,关于你的问题,计算,正在尝试将
字符串
类型添加到
十进制
类型。这个字符串可以是任何东西,包括字母和符号。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
子显示quizgrades()
将a设置为字符串()=quizGradesTextBox.Text.Split(““c”)
小测验作为十进制
“检查输入的分数不应该只是检查字符串的长度
'如果用户输入4个等级100(我不太可能知道),字符串的长度将为15
'您真正应该做的是检查拆分数组中有多少个元素,然后
'在对它们进行任何计算之前,请确保它们都是有效的数字。
'如果出现错误,请退出此子项,而不向列表中添加任何内容
如果a.计数>4,则
显示(“仅输入4个等级!”)
'如果有超过4个等级,则这是退出sub的线
出口接头
如果结束
将每个空格用作

如果(Val(space)>100)或Val(space)I将等级数(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
子显示quizgrades()
'去掉字符串中的空格
将a设置为字符串()=quizTextBox.Text.Split(““c”)
小测验作为“十进制”保存等级值
'输入的限制等级
如果a.计数>4,则
MessageBox.Show(“只有2个等级或更少!”)
出口接头
如果结束
将每个空格用作
如果(Val(间距)>100)或Val(间距)<0,则“限制坡度为0-100”
MessageBox.Show(“单个分数必须大于0且小于100!”)
出口接头
如果结束
测验+=CDec(空格)'在文本框中将数字相加
下一个
测验=CDec(测验*0.0375)“将分数乘以百分比,再除以文本框中的数字量
resultListBox.Items.Add(测验)
端接头

您应该使用调试器确保代码执行您认为/希望它执行的操作:
将每个空格作为
中的字符串,然后如果
空格
是字符串,那么如何将其添加到小数点
quick+=space
Put
Option Strict On
在代码文件的顶部您的代码工作正常,但在运行它时我遇到了另一个问题。最后的结果是,它将所有输入的数字相加,然后乘以.15,即“60”,但我试图找出如何将这些数字单独相乘,然后将最终值相加。我真的很抱歉,我发现我没有完全理解这一点。这难道不会给出同样的答案吗?即便如此,请参见我的最新答案。:)