我没有得到第二次输入数字的阶乘的正确答案

-1 c# factorial

第二次输入数字时,我没有得到阶乘的正确答案。

using System;

namespace Outcome2
{
    class Program
    {
        static void Main(string[] args)
        {
            int t, FactR = 1;
            do
            {
                Console.Write("Enter no.: ");
                t = Convert.ToInt32(Console.ReadLine());

                if (t < 0)
                    Console.WriteLine("Factorial of Negative number's can't be found ");
                else if (t <= 1)
                    Console.WriteLine("{0}! = {1}", t, FactR);
                else
                {
                    for (int counter = t; counter >= 2; counter--)
                    {
                        FactR = FactR * counter;
                    }
                    Console.WriteLine("{0}! = {1}", t, FactR);
                }

                Console.WriteLine("Do you wish to quit? Y/N");
            } while (Console.ReadKey().KeyChar != 'Y');

        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Alf*_*red 5

这可能是因为你没有重置FactR变量,你可以这样做。

     do{
       if (t < 0)
                Console.WriteLine("Factorial of Negative number's can't be found ");
            else if (t <= 1)
                Console.WriteLine("{0}! = {1}", t, FactR);
            else
            {
                FactR = 1;
                for (int counter = t; counter >= 2; counter--)
                {
                    FactR = FactR * counter;
                }
                Console.WriteLine("{0}! = {1}", t, FactR);
            }

            Console.WriteLine("Do you wish to quit? Y/N");
        } while (Console.ReadKey().KeyChar != 'Y');
Run Code Online (Sandbox Code Playgroud)