visual basic程序设计教程第四版答案(刘炳文)
VB编程:编写一个过程,用来计算并输出 S=1+1/2+1/3+……+1/100 的值。
解:这个题目可以通过循环来解决,这里给出用Do循环和For循环编写的两个过程
Function sumD(n As Integer) As Single
s = 1
i = 1
Do
i = i + 1
s = s + 1 / i
Loop Until i >= n
sumD = s
End Function
Function sumF(n As Integer) As Single
s = 0
For i = 1 To n
s = s + 1 / i
Next i
sumF = s
End Function
再编写窗体点击事件代码
Private Sub Form_Click()
sum1 = sumD(100)
sum2 = sumF(100)
Print
Print "sum1="; sum1
Print "sum2="; sum2
End Sub
运行程序,点击窗体,得到的结果是:
sum1=5.187377
sum2=5.187377
来源:visual basic程序设计教程第四版答案(刘炳文)
本文链接:http://www.wb98.com/vb1/post/vb_9.8.html
本站文章搜索: