菜鸟:计算器加法问题 在线等
原码如下:
Dim fuhao, answer
Private Sub Command1_Click()
Select Case fuhao
Case "+ "
answer = Text1.Text + Text2.Text
Case "- "
answer = Text1.Text - Text2.Text
Case "* "
answer = Text1.Text * Text2.Text
Case "/ "
answer = Text1.Text / Text2.Text
End Select
Text3.Text = answer
End Sub
Private Sub Command2_Click()
Unload Me
End Sub
Private Sub Option1_Click()
fuhao = "+ "
End Sub
Private Sub Option2_Click()
fuhao = "- "
End Sub
Private Sub Option3_Click()
fuhao = "* "
End Sub
Private Sub Option4_Click()
fuhao = "/ "
End Sub
除了+法外,其他正确输入看上去没有问题.
但加法返回错误值:
如输入:
1
2
得到12.
请问是怎么回事情.谢谢解答
[解决办法]
Case "+ "
answer = val(Text1.Text) + val(Text2.Text)
修改下这个语句
+ 符号左右均为字符串时,为连接操作,而非加法操作
[解决办法]
Case "/ "
if Text2.Text= "0 " then msgbox "不能为0 ":exit sub
answer = Text1.Text / Text2.Text
[解决办法]
做个if判断……如果除数=0就不进行运算然后弹出一个对话框之类的