VB计算器加号代码要怎么写啊?

2025-01-04 07:51:31
推荐回答(3个)
回答1:

程序的思路很单纯,不过要按你的形式写确实有些麻烦,因为你没办法存储两个自己输入的数
这个时候要用到mid函数,这是我按你给的界面写的代码,想得比较复杂,但是有用,你可以拿去试试
Dim x As Integer, y As Integer, sombol As String
Private Sub Command1_Click()
Text1.Text = Text1.Text & "1"
End Sub

Private Sub Command2_Click()
Text1.Text = Text1.Text & "2"
End Sub

Private Sub Command3_Click()
Text1.Text = Text1.Text & "3"
End Sub

Private Sub Command4_Click()
Text1.Text = Text1.Text & "+"
End Sub

Private Sub Command5_Click()
Text1.Text = Text1.Text & "-"
End Sub

Private Sub Command6_Click()
For i = 1 To Len(Text1.Text)
If Mid(Text1.Text, i, 1) = "+" Or Mid(Text1.Text, i, 1) = "-" Then
x = Val(Mid(Text1.Text, 1, i - 1))
sombol = Mid(Text1.Text, i, 1)
y = Val(Mid(Text1.Text, i + 1, Len(Text1.Text)))
End If
Next i
If sombol = "+" Then
Text1.Text = Text1.Text & "=" & x + y
Else
Text1.Text = Text1.Text & "=" & x - y
End If
End Sub

回答2:

+号不一样,如果是文本,则是&的功能,只有两个都是数字的时候,才执行加的功能

回答3:

例如text3.text=val(text1.text)+ val(text2.text)