if(i==N) i=0
sum +=value_buf[count]
你的主要错误有两处:1、在循环中
For j = i To i + 4
If j >n Then j = j - n
b(i) = b(i) + a(j)
Next j
语句If j >n Then j = j - n不断改变循环变量,造成死循环。
这样所计算的结果也是错误的。应改为
For j = i To i + 4
If j >n Then
b(i) = b(i) + a(j - n)
Else
b(i) = b(i) + a(j)
End If
Next j
2、变量重复定义Dim max As Integer
修改后的完整代码如下:
Option Explicit
Option Base 1
Dim a() As Integer, b() As Integer, c() As Double, n As Integer
Private Sub Command1_Click()
Dim i As Integer, j As Integer
n = InputBox("输入数据个数:", , 20)
ReDim a(n)
eDim b(n)
ReDim c(n)
Randomize
For i = 1 To n
a(i) = Int(21 * Rnd) + 10
Text1 = Text1 &a(i) &" "
If i Mod 10 = 0 Then
Text1 = Text1 &vbCrLf
End If
Next i
End Sub
Private Sub Command2_Click()
Dim i As Integer, j As Integer, p As Integer, q As Integer, max As Integer
For i = 1 To n
For j = i To i + 4
If j >n Then
b(i) = b(i) + a(j - n)
'j = j - n
Else
b(i) = b(i) + a(j)
End If
Next j
Next i
For i = 1 To n
c(i) = b(i) / 5
Next i
Call maxav(c, p, max)
Text2 = "av(" &p &")=" &max
End Sub
Private Sub maxav(a() As Double, p As Integer, max As Integer)
Dim i As Integer
max = a(1)
p = 1 '第一个可能就是最大的数
For i = 1 To n
If max <a(i) Then
max = a(i)
p = i
End If
Next
End Sub
Private Sub Command3_Click()
Text1 = ""
Text2 = ""
Command1.SetFocus
End Sub
Private Sub Command4_Click()
End
End Sub
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)