这是我的VB中计算器的代码!哪路的高手帮我看看哪里错了!谢谢拉!

来源:百度知道 编辑:UC知道 时间:2024/06/06 14:16:49
Private Sub Command1_Click(Index As Integer)
Dim a As Single
Dim b As Single
Dim sum As Single
Dim operate As Integer
If Index <= 9 Then
If Text1.Text = "" Then
Text1.Text = Index
Else
Text1.Text = Text1.Text & CStr(Index)
End If
End If
If Index = 10 Then
If InStr(Text1.Text, ".") = 0 Then
Text1.Text = Text1.Text & "."
End If
End If
If Index = 11 Then
Text1.Text = -1 * Text1.Text
End If
If Index >= 12 And Index <= 15 Then
a = Text1.Text
Text1.Text = ""
End If
If Index = 16 Then
b = Text1.Text
Select Case operate
Case 12
sum = a + b
Case 13
sum = a - b
Case 14
sum = a * b
Case 15
sum = a / b
End Select
Text1.Text = sum
End If
End Sub
问题是等于时总是为零!C11(+/-)C10为小数点

首先你的程序有两处错误
(1)四个变量应该为全局变量
(2)变量operate没有赋值
下面是正确的程序,你可以复制下来运行试试
Dim a As Single
Dim b As Single
Dim sum As Single
Dim operate As Integer
Private Sub Command1_Click(Index As Integer)
If Index <= 9 Then
If Text1.Text = "" Then
Text1.Text = Index
Else
Text1.Text = Text1.Text & CStr(Index)
End If
End If
If Index = 10 Then
If InStr(Text1.Text, ".") = 0 Then
Text1.Text = Text1.Text & "."
End If
End If
If Index = 11 Then
Text1.Text = -1 * Text1.Text
End If
If Index >= 12 And Index <= 15 Then
operate = Index
a = Text1.Text
Text1.Text = ""
End If
If Index = 16 Then
b = Text1.Text
Select Case operate
Case 12
sum = a + b
Case 13
sum = a - b
Case 14
sum = a * b
Case 15