模拟8位2进制的加一计数,(但是a(7)老是调试溢出。。。望指点)
Dim a(7) As IntegerDim i, j, k, num, flag
i = 0
num = 1'默认0为一个
flag = 0 '清空记位器
k = 7
For i = 0 To 7 '清0
a(i) = 0
Next i
Do While flag < 8 '当有8个1结束
a(7) = a(7) + 1 '从末位加1
For k = 7 To 1 '2进制进位
If a(k) > 1 Then
a(k) = 0
a(k - 1) = 1
num = num + 1 '计数
End If
Next k
flag = 0 '清空记位器
For j = 0 To 7 '记录有几个1
If a(j) = 1 Then
flag = flag + 1
End If
Next j
Loop
MsgBox "num"