-1

I am learning VB.NET 2008. I have come across a situation

I want to build a recursive program like factorial of a number or fibonacci series for 1st 50 terms, using tools for Windows in VB.NET 2008. Say eg., type a number in a text box, click on a button and the output of the factorial of the number will be displayed on a Label. The inner code should be implemented in a Recursive way and not by using simple loops only.

I am not finding a proper way to solve this.

Please, help me out.

Thanks a lot.

4

2 回答 2

0

你是这样尝试的吗?

    Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim x As Integer
        Dim Fac As Integer
        x = TextBox1.Text
        Fac = 1
        For i = x To 1 Step -1
            Fac = Fac * i
        Next i
        Label1.Text = Fac
    End Sub
End Class

希望有帮助

重新编辑以获得更好的性能:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) 处理 Button1.Click

Try
    Dim x As Long = (CLng(TextBox1.Text)))
    Dim Fac As Long
    Fac = 1

    For i = x To 1 Step -1
        Fac = Fac * i
    Next i

    Label1.Text = "Factorial Number:  " & Fac
Catch ex As Exception
    MsgBox(ex.Message.ToString)
End Try

结束子

sry 我没看到你现在可以使用它...我测试了它,最大数量是 20 :)

于 2013-08-10T16:35:34.533 回答
0

您可以使用调用自身的递归函数。

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Label1.Text = Factorial(CLng(TextBox1.Text))
End Sub
Function Factorial(ByVal number As Long) As Long
    If number <= 1 Then
        Return (1)
    Else
        Return number * Factorial(number - 1)
    End If
End Function
于 2013-08-10T18:14:41.217 回答