用VB编一个程序
答案:2 悬赏:50 手机版
解决时间 2021-05-23 08:06
- 提问者网友:寂寞撕碎了回忆
- 2021-05-22 15:37
用输入框输入一个数n,若此数是大于0的正整数,则求S=1 - 2/3 + 4/5 - ……2n/2n+1,否则要求重新输入数据,直到数据合理为止。
最佳答案
- 五星知识达人网友:英雄的欲望
- 2021-05-22 16:43
楼上 问问1队 团队的答案是错的。下面的代码是按题目的要求写的,如有疑问请追问。
Private Sub Command1_Click()
Dim n As Double
Dim s As Double
Dim f As Integer
Dim i As Integer
Do
n = Val(InputBox("输入N:"))
If n > 0 And Int(n) = n Then
Exit Do
Else
MsgBox "请输入正整数!"
End If
Loop While True
s = 1
f = -1
For i = 1 To n
s = s + f * 2 * i / (2 * i + 1)
f = -f
Next
MsgBox "S=" & s
End Sub
Private Sub Command1_Click()
Dim n As Double
Dim s As Double
Dim f As Integer
Dim i As Integer
Do
n = Val(InputBox("输入N:"))
If n > 0 And Int(n) = n Then
Exit Do
Else
MsgBox "请输入正整数!"
End If
Loop While True
s = 1
f = -1
For i = 1 To n
s = s + f * 2 * i / (2 * i + 1)
f = -f
Next
MsgBox "S=" & s
End Sub
全部回答
- 1楼网友:蓝房子
- 2021-05-22 17:31
Dim sum As Double, k As Double Dim n As Double Dim i As Integer n = CInt(InputBox("请输入n:"))
if n>0 and n=int(n) then k = 1 sum = 0 For i = 1 To n sum = sum + k / (2 * i - 1) k = -k Next i msgbox "sum = " sum
else
msgbox"请输入正整数",vbokonly
end if
这样就可以了
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯