使用VB6.0设计一个四则运算程序,输入两个操作数和一个操作符(+-*⼀),并计算出结果。

2025-03-12 23:59:56
推荐回答(2个)
回答1:

图片中没显示结果显示在哪,我就将结果在text3中输出,具体代码如下,有疑问,可追问:
Private Sub Form_Load()
Combo1.AddItem "+"
Combo1.AddItem "-"
Combo1.AddItem "*"
Combo1.AddItem "/"
End Sub
Private Sub Command1_Click()
If Combo1.Text = "+" Then
Text3.Text = Val(Text1.Text) + Val(Text2.Text)
ElseIf Combo1.Text = "-" Then
Text3.Text = Val(Text1.Text) - Val(Text2.Text)
ElseIf Combo1.Text = "*" Then
Text3.Text = Val(Text1.Text) * Val(Text2.Text)
ElseIf Combo1.Text = "/" Then
If Text3.Text = "0" Then
MsgBox "除数不能为0!"
Else
Text3.Text = Val(Text1.Text) / Val(Text2.Text)
End If
End If
End Sub

回答2:

假设按钮名称是command1,文本框名称分别是text1和text2,组合框名称是combo1,代码如下:

Private Sub Command1_Click()
If Combo1.Text = "+" Then MsgBox Val(Text1) + Val(Text2)
If Combo1.Text = "-" Then MsgBox Val(Text1) - Val(Text2)
If Combo1.Text = "*" Then MsgBox Val(Text1) * Val(Text2)
If Combo1.Text = "/" Then MsgBox Val(Text1) / Val(Text2)
End Sub