gpt4 book ai didi

vba - 阶乘函数返回平方数而不是阶乘

转载 作者:行者123 更新时间:2023-12-04 08:24:05 25 4
gpt4 key购买 nike

我的代码哪里错了?它返回任何数字的平方:

Sub factorial()
Dim x As Long, i As Integer, fact As Long
x = InputBox("enter the integer")
For i = 1 To x
fact = i * x
Next i
MsgBox fact
End Sub

最佳答案

练习循环和 If声明!?

Option Explicit

' If you are practicing (loops) then:
Sub factorial()
Dim x As Long, i As Long, fct As Double
x = InputBox("enter the integer")
If x >= 0 And x <= 170 Then
fct = 1
If x > 1 Then
For i = 2 To x
fct = fct * i
Next i
End If
MsgBox fct
Else
MsgBox "Next time enter a number between 0 and 170."
Exit Sub
End If
End Sub

' ...if not, just use Fact
Sub factorialExcel()
Dim x As Long
x = InputBox("enter the integer")
If x >= 0 And x <= 170 Then
MsgBox Application.WorksheetFunction.Fact(x)
Else
MsgBox "Next time enter a number between 0 and 170."
Exit Sub
End If
End Sub

关于vba - 阶乘函数返回平方数而不是阶乘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65357044/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com