我正在编写一个程序,它计算5个数字的阶乘,并以表格的形式输出结果,但我一直得到、零点、。
因子公式:.n!= n×(n-1)!
我试过:
CLS
DIM arr(5) AS INTEGER
FOR x = 1 TO 5
INPUT "Enter Factors: ", n
NEXT x
f = 1
FOR i = 1 TO arr(n)
f = f * i
NEXT i
PRINT
PRINT "The factorial of input numbers are:";
PRINT
FOR x = 1 TO n
PRINT f(x)
NEXT x
END我期待着:
Numbers Factorrials
5 120
3 6
6 720
8 40320
4 24发布于 2019-05-08 06:59:08
你犯了一些错误
FOR i = 1 TO arr(n)您也从未将实际值存储到arr中。
PRINT f(x)在这里,您从数组f中获取代码中也没有定义的
发布于 2019-05-08 14:12:38
我面前没有基本的翻译,但我想这就是你要找的:
CLS
DIM arr(5) AS INTEGER
DIM ans(5) AS LONG 'You need a separate array to store results in.
FOR x = 1 TO 5
INPUT "Enter Factors: ", arr(x)
NEXT x
FOR x = 1 to 5
f& = 1
FOR i = 1 TO arr(x)
f& = f& * i
NEXT i
ans(x) = f&
NEXT x
PRINT
PRINT "The factorial of input numbers are:";
PRINT
PRINT "Numbers", "Factorials"
FOR x = 1 TO 5
PRINT STR$(arr(x)), ans(x)
NEXT x
END不过,请注意:在编程中,除非内存不足,否则应该避免重用变量。它可以正确地完成,但是它为在更大的程序中很难找到bug创造了很多机会。
发布于 2019-05-09 02:58:18
计算阶乘数组的可能解决方案:
CLS
DIM arr(5) AS INTEGER
DIM ans(5) AS LONG
FOR x = 1 TO 5
INPUT "Enter Factors: ", arr(x)
f& = 1
FOR i = 1 TO arr(x)
f& = f& * i
NEXT i
ans(x) = f&
NEXT x
PRINT
PRINT "The factorial of input numbers are:";
PRINT
PRINT "Numbers", "Factorials"
FOR x = 1 TO 5
PRINT arr(x), ans(x)
NEXT x
ENDhttps://stackoverflow.com/questions/56031688
复制相似问题