我用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”的值为空。 任何人都可以帮我解决这个问题吗?
答案 0 :(得分:0)
我在一个功能中执行以下操作,它似乎对我有用。我在方法中运行代码时得到3,单击1,单击+,然后单击2.
我基本上把它的输出放到windows窗体的文本框中,当我运行这段代码时,它显示为3。
我确实添加了 Dim text1 As String 这一行,您是否在其他地方声明了这一点?
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 = ""
答案 1 :(得分:0)
在一般方法之外声明你的变量。这将使方法可用于获取/设置它们。另一种方法是将按钮值作为参数传递,但第一部分更容易!