
正文
vb.net阶乘代码 vbs求阶乘
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
程序高手来1~10的阶乘。用VB.NET
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
相关问答
Q1: VB编程,求阶乘的过程
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
Q2: 编写VB阶乘的函数
1、vb代码中,求一个数的阶乘的函数代码如下:
Public Function jiecheng(ByVal n As Integer) As Double
Dim jc As Double
jc = 1
For i = 1 To n
jc = jc * i
Next
jiecheng = jc
End Function
2、具体用法:首先打开VB6.0,新建一个标准exe工程;
3、在设计界面上添加一个Command控件,Command1的Caption属性设置为"N的阶乘"。
4、双击Command1控件进入代码编辑界面,编辑代码如下:
Public Function jiecheng(ByVal n As Integer) As Double
Dim jc As Double
jc = 1
For i = 1 To n
jc = jc * i
Next
jiecheng = jc
End Function
Private Sub Command1_Click()
Dim sum As Double, n As Integer
n = InputBox("计算的N的阶乘,请输入数字N")
sum = jiecheng(n)
MsgBox (sum)
End Sub
5、运行新建的工程,单击按钮,输入要计算的N的阶乘的数字N,得出阶乘结果。
Q3: VB.NET 怎样编写使用Do While 循环输出1-10的阶乘,我是新手请加上文字说明,呵呵,谢谢各位了!
首先在窗体上画两个控件: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.net阶乘代码和vbs求阶乘的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。








