为什么getArea((l / 3),(n-1)只返回null值?

时间:2012-09-09 15:45:39

标签: vb.net

为什么函数getArea返回NULL / Nothing

  Module Module3
  Dim noOfTriangles As Integer = (3 / 2)
  Sub main()
    Dim l As Single
    Dim n As Integer
    l = Console.ReadLine()
    n = Console.ReadLine()
    Console.WriteLine(getArea(l, n))
    Console.ReadKey()
  End Sub

  Function getArea(ByVal l As Single, ByVal n As Integer)
    Dim area As Single = 1
    If n = 0 Then
      Return Nothing
    Else
      noOfTriangles = noOfTriangles * 2
      Return (((3 ^ (1 / 2)) / 4) * (l ^ 2) + (noOfTriangles * getArea((l / 3), (n - 1))))
    End If
  End Function
End Module

1 个答案:

答案 0 :(得分:0)

试试这个,看看它是否能让你更接近你想要的东西:

    Module Module3
  Dim noOfTriangles As double = (3 / 2)
  Sub main()
    Dim l As Single
    Dim n As Integer
    l = Single.Parse(Console.ReadLine())
    n = Integer.Parse(Console.ReadLine())
    Console.WriteLine(getArea(l, n))
    Console.ReadKey()
  End Sub

  Function getArea(ByVal l As Single, ByVal n As Integer) as Double
    Dim area As Single = 1
    If n = 0 Then
      Return Nothing
    Else
      noOfTriangles = noOfTriangles * 2
      Return (((3 ^ (1 / 2)) / 4) * (l ^ 2) + (noOfTriangles * getArea((l / 3), (n - 1))))
    End If
  End Function
End Module