关于阶乘的VB程序

来源:百度知道 编辑:UC知道 时间:2024/06/21 09:52:36
Public Class Form1
Dim numCal As Integer
Dim product As Double
Dim i As Integer

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
numCal = Integer.Parse(TextBox1.Text)
If numCal = 1 Then
TextBox2.Text = 1
End If
For i = 2 To numCal
product = product * i
Next
TextBox2.Text = product.ToString
End Sub
End Class
求各位大侠看看错误在那。。我的结果框里面不管输什么都显示0.。

应为你没有给product 赋初值 VB里面默认的初值是0 接下来你知道吧

Public Class Form1
Dim numCal As Integer
Dim product As Integer=1
Dim i As Integer

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
numCal = Integer.Parse(TextBox1.Text)
If numCal = 1 Then
TextBox2.Text = 1
End If
For i = 2 To numCal
product = product * i
Next
TextBox2.Text = product.ToString
End Sub
End Class

在最开始的地方加
product = 1