新人有问题求解决,我想做一个跟windows附带的计算器一样的,老是出错,请各位帮忙看看

wdk8882006 2007-12-10 10:30:10
我想做一个计算器,但是老出错,输入到第6位的时候就不行了,还有"+","-"等等都不能用,以下是我写的,请各位帮帮忙指出其中的错误,我是新人!多多指教!
Dim firstnum As Double
Dim secondnum As Double
Dim char As Double

Private Sub Command1_Click()
Dim num As Integer
Dim newnum As Integer
num = Val(Text1.Text)
newnum = num * 10 + 1
Text1.Text = newnum

End Sub

Private Sub Command10_Click()
firstnum = Val(Text1.Text)
Text1.Text = 0
char = "+"
End Sub

Private Sub Command11_Click()
firstnum = Val(Text1.Text)
Text1.Text = 0
char = "/"
End Sub

Private Sub Command12_Click()
firstnum = Val(Text1.Text)
Text1.Text = 0
char = "*"
End Sub

Private Sub Command13_Click()
firstnum = Val(Text1.Text)
Text1.Text = 0
char = "-"
End Sub

Private Sub Command14_Click()
Dim result As Double
secondnum = Val(Text1.Text)
If char = "+" Then
result = firstnum + secondnum
If char = "-" Then
result = firstnum - secondnum
If char = "/" Then
result = firstnum / secondnum
If char = "*" Then
result = firstnum * secondnum
End If
Text1.Text = result
End Sub

Private Sub Command2_Click()
Dim num As Integer
Dim newnum As Integer
num = Val(Text1.Text)
newnum = num * 10 + 2
Text1.Text = newnum
End Sub

Private Sub Command3_Click()
Dim num As Integer
Dim newnum As Integer
num = Val(Text1.Text)
newnum = num * 10 + 3
Text1.Text = newnum
End Sub

Private Sub Command4_Click()
Dim num As Integer
Dim newnum As Integer
num = Val(Text1.Text)
newnum = num * 10 + 4
Text1.Text = newnum
End Sub

Private Sub Command5_Click()
Dim num As Integer
Dim newnum As Integer
num = Val(Text1.Text)
newnum = num * 10 + 5
Text1.Text = newnum
End Sub

Private Sub Command6_Click()
Dim num As Integer
Dim newnum As Integer
num = Val(Text1.Text)
newnum = num * 10 + 6
Text1.Text = newnum
End Sub

Private Sub Command7_Click()
Dim num As Integer
Dim newnum As Integer
num = Val(Text1.Text)
newnum = num * 10 + 7
Text1.Text = newnum
End Sub

Private Sub Command8_Click()
Dim num As Integer
Dim newnum As Integer
num = Val(Text1.Text)
newnum = num * 10 + 8
Text1.Text = newnum
End Sub

Private Sub Command9_Click()
Dim num As Integer
Dim newnum As Integer
num = Val(Text1.Text)
newnum = num * 10 + 9
Text1.Text = newnum
End Sub
...全文
16 点赞 收藏 2
写回复
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
白发程序猿 2007-12-11
看不懂写的是啥玩意
回复
绿洲 2007-12-11
1、请把所有的button命名。没有名字的button猜不出是什么。索性的是我猜出来:Command8可能是一个按钮“8”,不妨叫做cmdNum8
2、Dim char As Double: char是保留关键字,左变量会死的很惨。
3、如果char是个变量,那么 char="+" 一定是错的(不是定义了为Double了么?)。
4、Dim num As Integer: Integer类型最大可以表示多少位?Long类型呢?
5、num = Val(Text1.Text): 据我所知Val可能是转成Double的。你可想好了。
6、有没有必要内部存储数字,不要动不动就从控件往回找,这个效率可不是一般的差。
7、建议改变一下你的缩紧习惯:
==========
'全局变量嘛,缩进就免了
Dim firstnum As Double
Dim secondnum As Double
Dim char As Double

Private Sub Command1_Click()
Dim num As Integer
Dim newnum As Integer
'既然没有嵌套,缩进只能无谓增加无效空隙,何苦啊
num = Val(Text1.Text)
newnum = num * 10 + 1
Text1.Text = newnum
End Sub
......
==========
回复
发动态
发帖子
VB基础类
创建于2007-09-28

7453

社区成员

VB 基础类
申请成为版主
社区公告
暂无公告