【发布时间】:2015-03-13 07:35:38
【问题描述】:
我有这段代码从用户那里获取输入并计算其阶乘和小于输入数的阶乘,但我一直只获取第一个数字的阶乘,其余为 0。应该是这样的:例如,如果输入是5:
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; // assign n to num放在while循环里面