VB分别利用For和Do循环语句求s=1+1/2+1/4+1/7+1/11+1/16+1/22+……的值,当第i项的值
来源:学生作业帮助网 编辑:作业帮 时间:2024/11/03 01:22:37
VB分别利用For和Do循环语句求s=1+1/2+1/4+1/7+1/11+1/16+1/22+……的值,当第i项的值
VB分别利用For和Do循环语句求s=1+1/2+1/4+1/7+1/11+1/16+1/22+……的值,当第i项的值
VB分别利用For和Do循环语句求s=1+1/2+1/4+1/7+1/11+1/16+1/22+……的值,当第i项的值
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim x As Integer = 1, s As Single, i As Integer
Do While 1.0! / x > 10 ^ -5
s = s + 1.0! / x
i = i + 1
x = x + i
Loop
Label1.Text = s & "," & i
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim x As Integer = 1, s As Single, i As Integer
For i = 1 To 100000
If 1.0! / x < 10 ^ -5 Then Exit For
s = s + 1.0! / x
x = x + i
Next
Label2.Text = s & "," & i - 1
End Sub
End Class