我正在VB.NET Windows应用程序中创建一个计算器。所有运算符(加法和减法)都可以正常工作,但是,当我尝试使用除法或乘法进行计算时,第39行会抛出System.Data.SyntaxErrorException
。
这是引发异常的行:
Dim result = New DataTable().Compute(equation, Nothing)
是否有人知道为什么会发生这种情况?这是其余的代码:
Public Class Form1
Private Sub ButtonClickMethod(sender As Object, e As EventArgs) Handles num0.Click, num1.Click, num2.Click, num3.Click, num4.Click, num5.Click, num6.Click, num7.Click, num8.Click, num9.Click, opdivision.Click, opmultiply.Click, opdecimal.Click, opclear.Click, opminus.Click, opadd.Click, opequal.Click
Dim button As Button = CType(sender, Button)
If button.Name = "num1" Then
TextBox1.Text = TextBox1.Text + "1"
End If
If button.Name = "num2" Then
TextBox1.Text = TextBox1.Text + "2"
End If
If button.Name = "num3" Then
TextBox1.Text = TextBox1.Text + "3"
End If
If button.Name = "num4" Then
TextBox1.Text = TextBox1.Text + "4"
End If
If button.Name = "num5" Then
TextBox1.Text = TextBox1.Text + "5"
End If
If button.Name = "num6" Then
TextBox1.Text = TextBox1.Text + "6"
End If
If button.Name = "num7" Then
TextBox1.Text = TextBox1.Text + "7"
End If
If button.Name = "num8" Then
TextBox1.Text = TextBox1.Text + "8"
End If
If button.Name = "num9" Then
TextBox1.Text = TextBox1.Text + "9"
End If
If button.Name = "num0" Then
TextBox1.Text = TextBox1.Text + "0"
End If
If button.Name = "opdecimal" Then
TextBox1.Text = TextBox1.Text + "."
End If
If button.Name = "opequal" Then
Dim equation As String = TextBox1.Text
Dim result = New DataTable().Compute(equation, Nothing)
boxresult.Text = result
End If
If button.Name = "opminus" Then
TextBox1.Text = TextBox1.Text + "-"
boxoperator.Text = boxoperator.Text + "-"
End If
If button.Name = "opmultiply" Then
TextBox1.Text = TextBox1.Text + "x"
boxoperator.Text = boxoperator.Text + "x"
End If
If button.Name = "opdivision" Then
TextBox1.Text = TextBox1.Text + "÷"
boxoperator.Text = boxoperator.Text + "÷"
End If
If button.Name = "opadd" Then
TextBox1.Text = TextBox1.Text + "+"
boxoperator.Text = boxoperator.Text + "+"
End If
If button.Name = "opclear" Then
TextBox1.Clear()
End If
End Sub
Private Sub opbackspace_Click(sender As Object, e As EventArgs) Handles opbackspace.Click
TextBox1.Text = TextBox1.Text.Remove(TextBox1.Text.Count - 1)
End Sub
End Class
答案 0 :(得分:1)
您必须使用不同的字符进行乘法和除法:
If button.Name = "opmultiply" Then
TextBox1.Text = TextBox1.Text + "*"
boxoperator.Text = boxoperator.Text + "x"
End If
If button.Name = "opdivision" Then
TextBox1.Text = TextBox1.Text + "/"
boxoperator.Text = boxoperator.Text + "÷"
End If