编程,求算式1+1⼀2!+1⼀3!+1⼀4!+……前10项的和

用vb
2025-03-03 06:01:00
推荐回答(1个)
回答1:

Dim n As Long, i As Long, ii As Long, x As Double, y As Double, str As String

n = InputBox("您想计算前多少项的和呢?(默认前10项)")

n = Fix(n)

If n <= 0 Then n = 10

y = 0: str = ""

For i = 1 To n

  x = 1

  For ii = 1 To i

    x = x * ii

  Next ii

  y = y + 1 / x

  If i = 1 Then

    str = str & "1+"

  Else

    str = str & "1/" & i & "!+"

  End If

Next i

str = Left(str, Len(str) - 1)

MsgBox str & "=" & y, vbOKOnly, "输出结果"

【效果如图所示】