怎么写vb的阶乘代码

怎么写vb的阶乘代码,第1张

1、For语蔽缓句实现

Private Sub Command1_Click()

  Dim s As Long, n As Integer, i As Integer

  n = Val(Text1.Text)

  s = 1

  For i = 1 To n

      s = s * i

  Next i

  Label4.Caption = Str(s)

End Sub

2、Do While语句实现

Private Sub Command1_Click()

  Dim s As Long, n As Integer, i As Integer

  n = Val(Text1.Text)

  s = 1

  i = 1

  Do While i <= n

      s = s * i

      i = i + 1

  Loop

  Label4.Caption = Str(s)

End Sub

扩展资配散料:

1~10的培并氏阶乘的结果如下:

1!=1

2!=2*1=2

3!=3*2*1=6

4!=4*3*2*1=24

5!=5*4*3*2*1=120

6!=6*5*4*3*2*1=720

7!=7*6*5*4*3*2*1=5040

8!=8*7*6*5*4*3*2*1=40320

9!=9*8*7*6*5*4*3*2*1=362880

10!=10*9*8*7*6*5*4*3*2*1=3628800

在画面添悔尘加判前纯Text1和Command1

然后复制如下掘咐代码窗体

Private Sub Command1_Click()

Dim i As Integer

Dim dSum As Double

dSum = 1

For i = 1 To Val(Text1.Text)

dSum = dSum * i

Next

MsgBox dSum

End Sub


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12412216.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-25
下一篇 2023-05-25

发表评论

登录后才能评论

评论列表(0条)

保存