问题描述
我正在 microsoft visual studio 2012 中做一个项目,我正在尝试编写一个应用程序来确定模块平均值.
I am doing a project in microsoft visual studio 2012 and i am trying to write an application to determine the module average.
脚本如下:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles confirm.Click
Dim sum As Double
sum = CDbl(test.Text) * 50% + CDbl(project.Text) * 30% + CDbl(quiz.Text) * 30%
Dim modulemark As Double
modulemark = CDbl(CAmarks.Text) * 50% + CDbl(exam.Text) * 50%
Dim Grade As String
If sum < 40 Then
Grade = "F"
ElseIf sum >= 40 And modulemark < 65 And modulemark >= 40 Then
Grade = "C"
ElseIf sum >= 40 And modulemark < 75 And modulemark >= 65 Then
Grade = "B"
Else
Grade = "A"
End If
该脚本旨在在单击名为确认"的按钮后计算分数并给出评分.
The script is intended to calculate the marks and give a grade after clicking a button named "Confirm".
但是,当我尝试运行编码时,它说:
However, when i tried to run the coding it said:
Microsoft.VisualBasic.dll 中发生类型为System.InvalidCastException"的未处理异常
An unhandled exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll
附加信息:从字符串 "" 到类型 'Double' 的转换无效.
Additional information: Conversion from string "" to type 'Double' is not valid.
有人能看出哪里出了问题吗?我是 Visual Studio 的新手,感谢您的帮助.
can someone see what is wrong? i am new to Visual studio and i appreciate your help.
附言编辑了最近的脚本.
P.S. edited recent script.
附言感谢用户蒂姆"的脚本
P.S. Thank you for the user "Tim" for the script
看起来对象的名称有冲突.
It looks like the names of the objects conflicted.
推荐答案
正如 Idle_Mind 在他们的回答中所说,Double.TryParse 是要走的路.这提供了一种尝试将值转换为双精度值的安全方法.如果转换成功,该方法返回 true,结果双精度值在 out 参数中返回.如果转换失败,则返回false,返回默认值double(即0).
As Idle_Mind said in their answer, Double.TryParse is the way to go. This provides a safe way to attempt to convert a value to a double. If the conversion succeeds, the method returns true, and the resulting double is returned in the out parameter. If the conversion fails, false is returned and the default value of double (which is 0) is returned.
一个简单的例子:
Dim result As Double
Dim score As String = "75"
If Double.TryParse(score, result) Then
' result will be a double with the value of 75
Else
' The conversion attempt failed, and result will have a value of 0
End If
因此要将其应用于您的方法(无需验证,尽管 Idle_Mind 的回答提供了一个很好的方法):
So to apply that to your method (with no validation, though Idle_Mind's answer gives a good approach):
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles confirm.Click
Dim sum As Double
Dim modulemark As Double
Dim testScore As Double
Dim projectScore As Double
Dim quizScore As Double
Dim marks As Double
Dim examScore As Double
Double.TryParse(test.Text, testScore)
Double.TryParse(project.Text, projectScore)
Double.TryParse(quiz.Text, quizScore)
Double.TryParse(CAmarks.Text, marks)
Double.TryParse(exam.Text, examScore)
sum = (testScore * .5) + (projectScore * .3) + (quizScore * .3)
modulemark = (marks * .5) + (examScore * .5)
Dim Grade As String
If sum < 40 Then
Grade = "F"
ElseIf sum >= 40 And modulemark < 65 And modulemark >= 40 Then
Grade = "C"
ElseIf sum >= 40 And modulemark < 75 And modulemark >= 65 Then
Grade = "B"
Else
Grade = "A"
End If
End Sub
对以上代码的解释.
首先,声明了 6 个 Double
变量 - 这是必要的,因为 Double.TryParse
将 out
参数作为第二个参数,并且必须在使用之前声明.您可以使用一个变量(并重复使用它),但为简单起见,我为每个分数选择了一个.
First, 6 Double
variables are declared - this necessary because Double.TryParse
takes an out
parameter as the second argument, and that must be declared before its used. You could use one variable (and reuse it), but for simplicity I chose one for each score.
一旦分数被解析(成功与否),就会确定累积的加权总数.请注意,在应用权重修饰符时使用了括号,以确保运算符优先级不会给您带来预期之外的结果.
Once the scores have been parsed (successfully or not) the cumulative, weighted totals are determined. Note that parentheses were used when applying the weight modifier, to ensure operator precedence doesn't give you a result other than expected.
希望这可以为您澄清事情.
Hopefully this clarifies things for you.
这篇关于“从字符串转换""输入“双倍"无效"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!