我用Visual Basic编写了简单的计算器,但是当我调试代码时,它会出现问题并且剂量无法正确运行。
我的代码
Public Sub general()
Dim num1 As Long, num2 As Long
Dim result As Single
Dim op As String
End Sub
Private Sub Form_Load()
Text1 = " "
num1 = "0"
num2 = "0"
op = " "
one.Caption = "1"
two.Caption = "2"
three.Caption = "3"
four.Caption = "4"
five.Caption = "5"
six.Caption = "6"
seven.Caption = "7"
eight.Caption = "8"
nine.Caption = "9"
zero.Caption = "0"
clear11.Caption = "cls"
Equal12.Caption = "="
plus.Caption = "+"
min14.Caption = "-"
multi15.Caption = "*"
div16.Caption = "/"
lnx17.Caption = "1/x"
power18.Caption = "x^2"
arc19.Caption = "aqr(x)"
exit20.Caption = "Exit"
End Sub
Private Sub one_Click()
Text1 = Text1 + "1"
End Sub
Private Sub two_Click()
Text1 = Text1 + "2"
End Sub
Private Sub three_Click()
Text1 = Text1 + "3"
End Sub
Private Sub four_Click()
Text1 = Text1 + "4"
End Sub
Private Sub five_Click()
Text1 = Text1 + "5"
End Sub
Private Sub six_Click()
Text1 = Text1 + "6"
End Sub
Private Sub seven_Click()
Text1 = Text1 + "7"
End Sub
Private Sub eight_Click()
Text1 = Text1 + "8"
End Sub
Private Sub nine_Click()
Text1 = Text1 + "9"
End Sub
Private Sub zero_Click()
Text1 = Text1 + "0"
End Sub
Private Sub clear11_click() 'cls button
test1 = ""
num1 = "0"
num2 = "0"
op = ""
End Sub
Private Sub Equal12_click() 'Equal button
num2 = Val(Text1)
Select Case op
Case "+"
result = num1 + num2
Case "-"
result = num1 - num2
Case "*"
result = num1 * num2
Case "/"
result = num1 / num2
Case "1/x"
result = 1 / num1
Case "x^2"
result = num1 ^ 2
'Case "aqr(x)"
'result = aqr(num1)
End Select
Text1 = Str(result)
op = ""
End Sub
Private Sub plus_click()
num1 = Val(Text1)
op = "+"
Text1 = ""
End Sub
Private Sub min14_click()
num1 = Val(Text1)
op = "-"
Text1 = ""
End Sub
Private Sub multi15_click()
num1 = Val(Text1)
op = "*"
Text1 = ""
End Sub
Private Sub div16_click()
num1 = Val(Text1)
op = "/"
Text1 = ""
End Sub
Private Sub squareroot19_click() 'sqr(x)button
num1 = Val(Text)
result = Sqr(num1)
Text1 -Str(result)
End Sub
当我调试它时,我发现问题是" OP"变量,该变量不是以正确的方式设置,因为当我单击"等价"按钮并选择案例部分时," OP"的值是空的。有人可以帮助我如何解决吗?
我在一个功能中运行以下内容,似乎对我有用。在方法中运行代码以单击1,单击 ,然后单击2。
时,我会得到3。我基本上将其输出放入Windows表单上的文本框中,该窗口显示了3个代码时显示3。
我确实添加了行 dim Text1为字符串,您是否在其他地方声明此?
Dim num1 As Long, num2 As Long
Dim result As Single
Dim op As String
Dim text1 As String
text1 = " "
num1 = "0"
num2 = "0"
op = " "
text1 = text1 + "1"
num1 = Val(text1)
op = "+"
text1 = ""
text1 = text1 + "2"
num2 = Val(text1)
Select Case op
Case "+"
result = num1 + num2
Case "-"
result = num1 - num2
Case "*"
result = num1 * num2
Case "/"
result = num1 / num2
Case "1/x"
result = 1 / num1
Case "x^2"
result = num1 ^ 2
'Case "aqr(x)"
'result = aqr(num1)
End Select
text1 = Str(result)
op = ""
在常规方法之外声明您的变量。这将使这些方法可用于获取/设置它们。替代方案可能是将按钮值作为参数传递,但第一部分更容易!