VB分别利用For和Do循环语句求s=1+1/2+1/4+1/7+1/11+1/16+1/22+……的值,当第i项的值

豆豆爱乐乐 1年前 已收到1个回答 举报

yaotangchi 幼苗

共回答了18个问题采纳率:77.8% 举报

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

1年前

2
可能相似的问题
Copyright © 2022 YULUCN.COM - 雨露学习互助 - 16 q. 0.028 s. - webmaster@yulucn.com