这是我到目前为止所拥有的:
namespace factorials
{
class Program
{
static void Main(string[] args)
{
int number;
do
{
Console.WriteLine("What non-negative integer do you want to factorial?");
while (!int.TryParse(Console.ReadLine(), out number))
Console.WriteLine("Please enter a whole number only");
calculate(ref number);
} while (number >= 0);
Console.WriteLine("Please enter a non-negative number");
}
static void calculate(ref int number)
{
int factorial;
int counter;
for (counter = number; counter <= number; counter++)
{
factorial = number * number;
Console.WriteLine("The factorial of {0} is {1}", number, factorial);
}
}
}
}
现在它只给我数字的平方,而不是它们的阶乘。如何让它重复输入的次数,从而产生阶乘?
另外我不确定是否有必要将程序限制为仅非负整数,但如果我想要那部分只是在那里结束程序而不是循环回到开头。