Public Class Form1
Dim randomNumberOne As New Random
Dim randomNumberTwo As New Random
Dim ChildGrade As Integer
Dim strChildGrade As Integer
Dim strcorrectCounter As Integer
Dim Correctcounter As Integer = 0
Dim WrongCounter As Integer = 0
Dim totalQuestions As Integer = 0
Dim percent As Integer = 0
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ChildGrade = InputBox("Enter your age ", "age")
grade()
FirstNumberlbl.Text = randomNumberOne.Next(0, 12)
SecondNumberlbl.Text = randomNumberOne.Next(0, 12)
End Sub
Private Sub grade()
If ChildGrade <= 4 Then
Gradelbl.Text = "Preschool"
ElseIf ChildGrade = 5 Then
Gradelbl.Text = "Kindergarden"
ElseIf ChildGrade <= 10 Then
Gradelbl.Text = "Elementary"
ElseIf ChildGrade <= 13 Then
Gradelbl.Text = "Middle School"
ElseIf ChildGrade <= 18 Then
Gradelbl.Text = "High School"
ElseIf ChildGrade > 18 Then
Gradelbl.Text = "NA"
End If
End Sub
Private Sub RandomNumberToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles RandomNumberToolStripMenuItem.Click
NumbersGroupBox.Enabled = False
FirstNumberlbl.Text = randomNumberOne.Next(0, 12)
SecondNumberlbl.Text = randomNumberTwo.Next(0, 12)
End Sub
Private Sub SingleTableToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SingleTableToolStripMenuItem.Click
NumbersGroupBox.Enabled = True
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
FirstNumberlbl.Text = "1"
SecondNumberlbl.Text = randomNumberTwo.Next(0, 12)
TextBox1.Focus()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
FirstNumberlbl.Text = "2"
SecondNumberlbl.Text = randomNumberTwo.Next(0, 12)
TextBox1.Focus()
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
FirstNumberlbl.Text = "3"
SecondNumberlbl.Text = randomNumberTwo.Next(0, 12)
TextBox1.Focus()
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
FirstNumberlbl.Text = "4"
SecondNumberlbl.Text = randomNumberTwo.Next(0, 12)
TextBox1.Focus()
End Sub
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
FirstNumberlbl.Text = "5"
SecondNumberlbl.Text = randomNumberTwo.Next(0, 12)
TextBox1.Focus()
End Sub
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
FirstNumberlbl.Text = "6"
SecondNumberlbl.Text = randomNumberTwo.Next(0, 12)
TextBox1.Focus()
End Sub
Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
FirstNumberlbl.Text = "7"
SecondNumberlbl.Text = randomNumberTwo.Next(0, 12)
TextBox1.Focus()
End Sub
Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
FirstNumberlbl.Text = "8"
SecondNumberlbl.Text = randomNumberTwo.Next(0, 12)
TextBox1.Focus()
End Sub
Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click
FirstNumberlbl.Text = "9"
SecondNumberlbl.Text = randomNumberTwo.Next(0, 12)
TextBox1.Focus()
End Sub
Private Sub Button10_Click(sender As Object, e As EventArgs) Handles Button10.Click
FirstNumberlbl.Text = "10"
SecondNumberlbl.Text = randomNumberTwo.Next(0, 12)
TextBox1.Focus()
End Sub
Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click
FirstNumberlbl.Text = "11"
SecondNumberlbl.Text = randomNumberTwo.Next(0, 12)
TextBox1.Focus()
End Sub
Private Sub Button12_Click(sender As Object, e As EventArgs) Handles Button12.Click
FirstNumberlbl.Text = "12"
SecondNumberlbl.Text = randomNumberTwo.Next(0, 12)
TextBox1.Focus()
End Sub
Private Sub CheckAnswer()
If TextBox1.Text <> (FirstNumberlbl.Text * SecondNumberlbl.Text) Then
MsgBox("Please try again", MsgBoxStyle.Information, "Answer")
WrongCounter = WrongCounter + 1
If WrongCounter = 2 Then
MsgBox("The correct answer is " & FirstNumberlbl.Text * SecondNumberlbl.Text, MsgBoxStyle.Information, "Correct answer")
WrongCounter = 0
FirstNumberlbl.Text = randomNumberOne.Next(0, 12)
SecondNumberlbl.Text = randomNumberTwo.Next(0, 12)
End If
TextBox1.Text = ""
End If
End Sub
Private Sub rightAnswer()
If FirstNumberlbl.Text * SecondNumberlbl.Text = TextBox1.Text Then
Correctcounter = Correctcounter + 1
NumberCorrectlbl.Text = Correctcounter
TextBox1.Text = ""
FirstNumberlbl.Text = randomNumberOne.Next(0, 12)
SecondNumberlbl.Text = randomNumberTwo.Next(0, 12)
End If
totalQuestions = totalQuestions + 1
End Sub
Private Sub Enterbtn_Click(sender As Object, e As EventArgs) Handles Enterbtn.Click
CheckAnswer()
rightAnswer()
TotalCountlbl.Text = totalQuestions
PercentCorrectlbl.Text = ((Correctcounter * 100) / (totalQuestions * 100)) * 100
End Sub
结束班级
我正在VB中创建一个乘法应用程序,该应用程序统计用户正确的问题数和错误的问题数。当我运行该程序时,它可以正确地计算正确的数量并累加百分比,但是当我测试它们何时出错时,我会收到此错误。 enter image description here
如果图像不可见,则错误提示, Microsoft.VisualBasic.dll中发生了'System.InvalidCastException'类型的未处理异常
其他信息:从字符串“”到类型“ Double”的转换无效
程序指出了问题所在,位于rightAnswer()子类的if语句中,但是我不确定它到底出了什么问题。
答案 0 :(得分:0)
问题是您正在尝试使用字符串进行计算。字符串是文本,而不是数字。
这不是一个完整的答案,但足以使您了解问题并更改代码以解决问题。
请勿对文本框和/或标签的内容进行任何计算。对此类任务使用正确的变量。使用控件可以简单地检索和显示。从长远来看,这将为您节省很多心痛。
例如,不要:
If TextBox1.Text <> (FirstNumberlbl.Text * SecondNumberlbl.Text) Then
您正在检查文本框中的数字以根据两个标签进行计算,它们都是文本!
因此,请按照适合目的的变量(例如整数)进行计算:
示例:
Dim FirstNumber As Integer
Dim SecondNumber As Integer
将它们用于计算,然后将结果显示为文本。
如果期望浮点数(十进制),请根据需要更改变量类型。
在“ CheckAnswer”子例程中,您应该按照以下思路开始思考:
If Not String.IsNullOrWhiteSpace(TextBox1.Text) Then
'We have something in the TextBox (you can check to allow only numbers to be entered in the TextBox.KeyDown/Press event)
If Not Integer.Parse(TextBox1.Text) = (FirstNumber * SecondNumber) Then
'Do what you need ...
End If
End If
还有一些其他事情可以简化您的代码。
您不需要12个按钮事件都在做同一件事。
在您的Form1_Load事件中输入以下内容:
Dim bTags() As Button = {Button1, Button2, Button3, Button4, Button5, Button6, Button7, Button8, Button9, Button10, Button11, Button12}
For t As Integer = 0 To bTags.Count - 1
bTags(t).Tag = t + 1
Next
然后删除所有12个Button Click事件并将其替换为:
Private Sub Buttons_Click(sender As Object, e As EventArgs) Handles Button9.Click, Button8.Click, Button7.Click, Button6.Click, Button5.Click, Button4.Click, Button3.Click, Button2.Click, Button12.Click, Button11.Click, Button10.Click, Button1.Click
Dim selectedButton As Button = CType(sender, Button)
FirstNumberlbl.Text = selectedButton.Tag.ToString
SecondNumberlbl.Text = randomNumberTwo.Next(0, 12)
TextBox1.Focus()
End Sub
工作完成...
此外,对子程序进行评分最好将其作为Select Case
函数:
在您的Form1_Load
中,将grade()
替换为Gradelbl.Text = Grade()
,并将您的grade()
子例程替换为:
Private Function Grade() As String
Select Case ChildGrade
Case < 5 : Return "Preschool"
Case = 5 : Return "Kindergarden"
Case < 11 : Return "Elementary"
Case < 14 : Return "Middle School"
Case < 19 : Return "High School"
Case Else : Return "N/A"
End Select
End Function
我希望其中的一些建议会有所帮助。
答案 1 :(得分:0)
您需要执行的是强制转换文本框的值。例如:
Convert.toInt32(FirstNumberlbl.Text)
或
Dim firstNumber as Integer
Integer.TryParse(FirstNumberlbl.Text, firstNumber)
或将一个变量分配给文本框,然后将其转换为文本框。