Private Sub Command1_Click()
 Dim a()
 a = Array(7, 8, 9, 10)
 For k = 0 To 2 Step 0.5
 s = s + a(k)
 Next k
 Print s
 
End Sub等于多少? 我觉得应该是39啊

解决方案 »

  1.   

    '将您的代码加上调试输出:
    Private Sub Command1_Click()
     Dim a()
     a = Array(7, 8, 9, 10)
     For k = 0 To 2 Step 0.5
     s = s + a(k)
       times = times + 1
     Debug.Print "times=" & times
      Debug.Print "k=" & k
     Debug.Print "a(k)=" & a(k)
     Debug.Print "s=" & s
     Debug.Print ""
     Next k
     Print s
     
    End Sub'调试输出的结果是:
    times=1
    k=0
    a(k)=7
    s=7times=2
    k=.5
    a(k)=7
    s=14times=3
    k=1
    a(k)=8
    s=22times=4
    k=1.5
    a(k)=9
    s=31times=5
    k=2
    a(k)=9
    s=40为什么您觉得应该是39啊?
    是应该等于40
      

  2.   

    k=.5时,a(k)=7
    那为什么k=1.5时,a(k)=9呢?
      

  3.   

    VB6数组的BUG呀,VB6的数值运算很弱的,你不知道吗?
      

  4.   

    CInt differs from the Fix and Int functions, which truncate rather than round the fractional part of a number. When the fractional part is exactly 0.5, CInt always rounds it to the nearest even whole number. For example, 0.5 rounds down to 0, and 1.5 rounds up to 2.
    也就是CInt和Fix与Int方法不同
    CInt对.5的进位方法是向偶数进位
    所以cint(0.5)=0 cint(1.5)=2
      

  5.   

    四舍五入问题:
    它的执行过程如下:
    k, a(k), s
    0,   7,   7
    0.5, 7,  14
    1,   8,  22
    1.5, 9,  31
    2,   9,  40s=40