Private Sub Cmd3_Click()Dim p As Integer,I As Integerp = 1For I = 1 To 5p = p + (2 * I - 1) / (2 * I + 1)If p >= 20 Then Exit ForNext IPrint I,pEnd Sub6 5

来源:学生作业帮助网 编辑:六六作业网 时间:2024/12/27 13:45:03
PrivateSubCmd3_Click()DimpAsInteger,IAsIntegerp=1ForI=1To5p=p+(2*I-1)/(2*I+1)Ifp>=20ThenExitForNextI

Private Sub Cmd3_Click()Dim p As Integer,I As Integerp = 1For I = 1 To 5p = p + (2 * I - 1) / (2 * I + 1)If p >= 20 Then Exit ForNext IPrint I,pEnd Sub6 5
Private Sub Cmd3_Click()
Dim p As Integer,I As Integer
p = 1
For I = 1 To 5
p = p + (2 * I - 1) / (2 * I + 1)
If p >= 20 Then Exit For
Next I
Print I,p
End Sub
6 5

Private Sub Cmd3_Click()Dim p As Integer,I As Integerp = 1For I = 1 To 5p = p + (2 * I - 1) / (2 * I + 1)If p >= 20 Then Exit ForNext IPrint I,pEnd Sub6 5
i=1 时 p=1+1/3=1
四舍五入1/3=0,下面的都等于1
i=2 时 p=1+3/5=2
i=3 时 p=2+5/7=3
i=4 时 p=3+7/9=4
i=5时 p=4+9/11=5 这时p=5
next i使i=6退出循环,输出6和5
如果你有vb可以运行下面的代码,在立即窗口可以看到每一部的值
Private Sub Cmd3_Click()
Dim p As Integer, I As Integer
p = 1
For I = 1 To 5
Debug.Print (2 * I - 1) / (2 * I + 1)
p = p + (2 * I - 1) / (2 * I + 1)
Debug.Print p
If p >= 20 Then Exit For
Debug.Print I, p
Next I
Debug.Print I, p
End Sub