十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
Public Class Form1
成都创新互联服务项目包括鹰手营子网站建设、鹰手营子网站制作、鹰手营子网页制作以及鹰手营子网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,鹰手营子网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到鹰手营子省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!
Inherits System.Windows.Forms.Form#Region " Windows 窗体设计器生成的代码 " Public Sub New()
MyBase.New() '该调用是 Windows 窗体设计器所必需的。
InitializeComponent() '在 InitializeComponent() 调用之后添加任何初始化 End Sub '窗体重写 dispose 以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub 'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer '注意: 以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
System.Diagnostics.DebuggerStepThrough() Private Sub InitializeComponent()
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Name = "Form1"
Me.Text = "Form1" End Sub#End Region Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim s As String
Dim i As Integer, f As Long
For i = 0 To 10
f = Factor(i)
s = s i "!=" f vbNewLine
Next i
MsgBox(s)
End Sub
Function Factor(ByVal n As Integer) As Long
End Function
End Class
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
Private Sub Command1_Click()
Dim A, B, C
A = 10
B = 3
C = f(A) / (f(B) * f(A - B))
Print C
End Sub
Private Function f(n)
Dim I As Integer
f = 1
For I = 1 To n
f = f * I
Next I
End Function
首先在窗体上画两个控件:TextBox1和Button1
TextBox1用来输入需要计算那个数的阶乘
双击Button1进入输入代码,代码如下
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Dim Factorial As Integer = 1 '定义一个变量用来记录阶乘的结果
Dim count As Integer '定义一个变量用来记录需要计算那个数的阶乘
Dim i As Integer = 1 '定义一个数用来循环
count = Int(Val(Me.TextBox1.Text)) '把TextBox1的值赋值给count
Do While i = count '下面开始计算阶乘
Factorial = Factorial * i '计算阶乘
i += 1 '自增1
Loop
MessageBox.Show(Int(Val(Me.TextBox1.Text)) "的阶乘是:" Factorial, "完成", MessageBoxButtons.OK) '弹出计算结果
Catch ex As Exception '出错提示
MessageBox.Show(Err.Description, "出错了", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
VB求阶乘需要Function 过程来实现。
Function 语句,声明 Function 过程的名称,参数以及构成其主体的代码。
以下是求输入数的阶乘代码:
Option Explicit
Dim Sum As Double
Dim N As Integer
Dim i As Integer
Private Function fact(N As Integer) As Double
fact = 1
Do While N 0
fact = fact * N
N = N - 1
Loop
End Function
Private Sub Command1_Click()
N = Val(Text1.Text)
Sum = fact(N)
Text2 = Sum
End Sub
Private Sub Form_Load()
Text1 = "": Text2 = ""
End Sub