我有一段代码,从用户那里获取输入并计算其阶乘以及小于输入数字的阶乘,但我一直只得到第一个数字的阶乘,而其他的数字都是0。应该像这样:例如,如果输入是5:
5! = 120
4! = 24
3! = 6
2! = 4
1! = 1
如何使循环遍历所有小于输入数字的数字?
5! = 120
4! = 24
3! = 6
2! = 4
1! = 1
如何使循环遍历所有小于输入数字的数字?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace multiple_factorials
{
class Program
{
static void Main(string[] args)
{
int num, n;
Console.WriteLine(".....................[Application 1].....................\n\n");
Console.WriteLine("Please enter a number to get its factorial: ");
num = Convert.ToInt32(Console.ReadLine());
n = num; // Assign n to num
while (num > 0)
{
for (int i = n - 1; i > 0; i--)
{
n *= i;
}
Console.WriteLine("Factorial of {0}! = {1}\n", num, n);
num--;
}
}
}
}
n = num; // 将 n 赋值为 num
放在 while 循环内部。 - Eric