Private Sub Form_Click()
Dim S As String, i As Integer, n(9) As Integer
Dim S1 As String * 1, j As Integer
S = Trim("12345a307291b233")
For i = 1 To Len(S)
S1 = Mid(S, i, 1)
If S1 >= "0" And S1 <= "9" Then
j = Val(S1)
n(j) = n(j) + 1
End If
Next i
For j = 0 To 9
Print j; ":"; n(j)
Next j
End Sub这个程序中n(j)的值是怎么算的 为什么最后的运算结果是
0:1
1:2
2:3
3:4
4:1
5:1
6:0
7:1
8:0
9:1
请讲一下算的过程
Dim S As String, i As Integer, n(9) As Integer
Dim S1 As String * 1, j As Integer
S = Trim("12345a307291b233")
For i = 1 To Len(S)
S1 = Mid(S, i, 1)
If S1 >= "0" And S1 <= "9" Then
j = Val(S1)
n(j) = n(j) + 1
End If
Next i
For j = 0 To 9
Print j; ":"; n(j)
Next j
End Sub这个程序中n(j)的值是怎么算的 为什么最后的运算结果是
0:1
1:2
2:3
3:4
4:1
5:1
6:0
7:1
8:0
9:1
请讲一下算的过程
这段代码是计算字符串 "12345a307291b233" 中 0~9分别出现了多少次。然后分析它是如何实现的?
这个过程已经是很简单的了。
如果你看代码不能明白,你只有在 Private Sub Form_Click() 处设置一个断点,运行程序,用鼠标左键点一下窗体。接着代码的执行过程被 IDE 中断,你按功能键 F8 ,一步一步看它的执行过程,每一句执行后达到什么效果。
如果这样你还是弄不明白,我看你还是..........
Dim S As String, i As Integer, n(9) As Integer
Dim S1 As String * 1, j As Integer
S = Trim("12345a307291b233")
For i = 1 To Len(S)
S1 = Mid(S, i, 1)
If S1 >= "0" And S1 <= "9" Then
j = Val(S1)
n(j) = n(j) + 1
End If
Next i
For j = 0 To 9
Print j; ":"; n(j)
Next j
End Sub 我知道i=1to16,是不是下面这样子啊
i=1,s1=1,S1 >= "0" And S1 <= "9" j=1 n(j)=n(j)+1=0+1=1
i=2,s1=2,S1 >= "0" And S1 <= "9" j=2 n(j)=n(j)+1=1+1=2
i=3,s1=3,S1 >= "0" And S1 <= "9" j=3 n(j)=n(j)+1=2+1=3
i=4,s1=4,S1 >= "0" And S1 <= "9" j=4 n(j)=n(j)+1=3+1=4
i=6,s1=a,不满足S1 >= "0" And S1 <= "9" end if
i=7,s1=3,S1 >= "0" And S1 <= "9" j=3 n(j)=n(j)+1=5+1=6
i=8,s1=0,S1 >= "0" And S1 <= "9" j=0 n(j)=n(j)+1=6+1=7
i=9,s1=7,S1 >= "0" And S1 <= "9" j=7 n(j)=n(j)+1=7+1=8
i=10,s1=2,S1 >= "0" And S1 <= "9" j=2 n(j)=n(j)+1=8+1=9i=12,s1=1,S1 >= "0" And S1 <= "9" j=1 n(j)=n(j)+1=10+1=11
i=13,s1=b,不满足S1 >= "0" And S1 <= "9" end if
i=14,s1=2,S1 >= "0" And S1 <= "9" j=2 n(j)=n(j)+1=11+1=12
i=15,s1=3,S1 >= "0" And S1 <= "9" j=3 n(j)=n(j)+1=12+1=13
然后执行For j = 0 To 9
Print j; ":"; n(j)
Next j
是0:n(j)=7 (对应的i=8,s1=0,S1 >= "0" And S1 <= "9" j=0 n(j)=n(j)+1=6+1=7)
1: n(j)=1(对应的i=1,s1=1,S1 >= "0" And S1 <= "9" j=1 n(j)=n(j)+1=0+1=1)
2: n(j)=12(i=14,s1=2,S1 >= "0" And S1 <= "9" j=2 n(j)=n(j)+1=11+1=12)
3: n(j)=14(i=16,s1=3,S1 >= "0" And S1 <= "9" j=3 n(j)=n(j)+1=13+1=14)
4: n(j)=4(i=4,s1=4,S1 >= "0" And S1 <= "9" j=4 n(j)=n(j)+1=3+1=4)
5: n(j)=5(i=5,s1=5,S1 >= "0" And S1 <= "9" j=5 n(j)=n(j)+1=4+1=5)
6: n(j)=0(因为没有对应的j=6,所以为0)
7: n(j)=8(i=9,s1=7,S1 >= "0" And S1 <= "9" j=7 n(j)=n(j)+1=7+1=8)
8: n(j)=0(因为没有对应的j=8,所以为0)
9: n(j)=10(i=11,s1=9,S1 >= "0" And S1 <= "9" j=9 n(j)=n(j)+1=9+1=10)
我这样计算有哪里不对吗?
肯定不对啊!
Dim S As S..... , n(9) As Integer
这里声明了一个整数类型的数组 n ,共10元素(默认下标从0开始),分别是n(0)、n(1)、......n(9)然后就是这些(注意红色字,黑色字是你 4F 的内容):
i=1,s1=1,S1 >= "0" And S1 <= "9" j=1 n(j)=n(j)+1=0+1=1 是n(1) =0+1= 1
i=2,s1=2,S1 >= "0" And S1 <= "9" j=2 n(j)=n(j)+1=1+1=2 是n(2) =0+1= 1
i=3,s1=3,S1 >= "0" And S1 <= "9" j=3 n(j)=n(j)+1=2+1=3 是n(3) =0+1= 1
i=4,s1=4,S1 >= "0" And S1 <= "9" j=4 n(j)=n(j)+1=3+1=4 是n(4) =0+1= 1
i=5, S1="5", j=5 n(5) =0+1= 1
i=6,s1=a,不满足S1 >= "0" And S1 <= "9" end if
i=7,s1=3,S1 >= "0" And S1 <= "9" j=3 n(j)=n(j)+1=5+1=6 是n(3) =1+1= 2
i=8,s1=0,S1 >= "0" And S1 <= "9" j=0 n(j)=n(j)+1=6+1=7 是n(0) =0+1= 1
i=9,s1=7,S1 >= "0" And S1 <= "9" j=7 n(j)=n(j)+1=7+1=8 是n(7) =0+1= 1
i=10,s1=2,S1 >= "0" And S1 <= "9" j=2 n(j)=n(j)+1=8+1=9 是n(2) =1+1= 2
i=11, S1="9", j=9 n(9) =0+1= 1
i=12,s1=1,S1 >= "0" And S1 <= "9" j=1 n(j)=n(j)+1=10+1=11 是n(1) =1+1= 2
i=13,s1=b,不满足S1 >= "0" And S1 <= "9" end if
i=14,s1=2,S1 >= "0" And S1 <= "9" j=2 n(j)=n(j)+1=11+1=12 是n(2) =2+1= 3
i=15,s1=3,S1 >= "0" And S1 <= "9" j=3 n(j)=n(j)+1=12+1=13 是n(3) =2+1= 3
i=16, S1="3", j=3 n(3) =3+1= 4后面一个 For 循环我也不想再说了.............
所有数值类型(Integer、Byte、Long、Double等):0
变长字符串:vbNullString
定长字符串:'\0'填充
Object : Nothing
Variant : Empty
........
自己还是多看点基础方面的书吧........