求1!+2!+3!+4!……+100!,程序如下Private Sub Form_Load()
    Dim i,  sum, temp As Integer
    sum = 0
    temp = 1
    For i = 1 To 100
        temp = temp * i          '说这行有溢出错误
        sum = sum + temp
    Next i
    Print sum
    Text1.Text = sum
End Sub这段程序有错误,十分不解,请教各位高手错误的原因,与修改方法,十分感谢。

解决方案 »

  1.   

    查查书,Integer类型的最大值是多少!!!
      

  2.   

    temp不要用整型,除了i都定义成double型吧
     
    Dim i As Integer,  sum As Double, temp As Double
    不要省略As Double
      

  3.   

    直接Dim i As Double就不用转换了:Private Sub Command1_Click()
        Dim i As Double
        Dim sum As Double
        Dim temp As Double
        
        sum = 0
        temp = 1
        For i = 1 To 100
            temp = (temp) * i
            sum = sum + temp
        Next i
        Print sum
        End Sub
      

  4.   

    改成这样就行啦
    Private Sub Command1_Click()
        Dim i As Integer
        Dim sum As Double
        Dim temp As Double
        
        sum = 0
        temp = 1
        For i = 1 To 100
            temp = (temp) * CDbl(i)
            sum = sum + temp
        Next i
        Print sum
        Text1.Text = sumEnd Sub
    Long型的范围为-2147483648 到 + 2147483647 长度还是不够而 Double 双精度型的范围为 +/-5E-324 到+/-1E308CDbl是用于类型转换的,如此处不进行类型转换,系统会把结果认为是整型数也会溢出的