计算一个数的阶乘

3
当我输入数字6来计算其阶乘时,它返回30(这是错误的)。
为什么我的程序会产生错误的输出?
using System;

namespace Scenario1_2
{
    class Program
    { 
        static void Main(string[] args)
        {
            int counter, number, fact;

            Console.WriteLine("Please enter the number you wish to factorize");
            number = int.Parse(Console.ReadLine());
            fact = number;

            for (counter = number - 1; counter >= 1; counter--)
            {
                fact = fact * counter;

                Console.WriteLine("The number you entered was {0} and it's factorial is {1}", number, fact);
                Console.ReadLine();
            }
        } 
    }
}

3
将你的console.writeline和readline移出循环,或者一直按回车键直到得到答案。 - Ian Mercer
哦,我明白了!非常感谢,那很有道理! - WarriorPrincessM
尝试使用此链接:https://dotnetfiddle.net/thhGTa - sujith karivelil
https://dev59.com/HWQn5IYBdhLWcg3wxZgn - Pavlo Vasilikiv
8个回答

7

看起来你对编程或者至少是C#比较新,那么为了好玩,这会让你大开眼界:

using System;

namespace Scenario1_2
{
    class Program
    { 
        static void Main(string[] args)
        {   
            Console.WriteLine("Please enter the number you wish to factorize");
            int number = int.Parse(Console.ReadLine());
            Console.WriteLine("The number you entered was {0} and it's factorial is {1}", number, Factorial(number));
            Console.ReadKey(true);
        }

        static int Factorial(int n)
        {
           if (n >= 2) return n * Factorial(n - 1);
           return 1;
        } 
    }
}  

没有循环,函数调用自身
你也可以这样做:
using System;

namespace Scenario1_2
{
    class Program
    { 
        static void Main(string[] args)
        {   
            Console.WriteLine("Please enter the number you wish to factorize");
            int number = int.Parse(Console.ReadLine());
            Console.WriteLine("The number you entered was {0} and it's factorial is {1}", number, Factorial(number));
            Console.ReadKey(true);
        }

        static int Factorial(int n)
        {
           return Enumerable.Range(1, n).Aggregate((i, r) => r * i);
        } 
    }
}

这有点混乱 :) ...但它确实将重要的工作简化为了一行代码。

接下来是我个人最喜欢的,无限枚举:

using System;

namespace Scenario1_2
{
    class Program
    { 
        static void Main(string[] args)
        {   
            Console.WriteLine("Please enter the number you wish to factorize");
            int number = int.Parse(Console.ReadLine());
            Console.WriteLine("The number you entered was {0} and it's factorial is {1}", number, Factorials().Skip(number-1).First());
            Console.ReadKey(true);
        }

        static IEnumerable<int> Factorials()
        {
            int n = 1, f = 1;
            while (true) yield return f = f * n++;
        } 
    }
}

哇,非常感谢您提供这些精彩的示例!学习它们让我有了一些重要的见解。它实际上还帮助我更好地理解递归!无法感谢您的足够!! - WarriorPrincessM

1
程序已暂停等待某些输入。您需要将第二个Console.ReadLine()移出循环。并且可能还要移除Console.WriteLine(),除非您想看到每次迭代的完成情况。

0

你需要将两行代码从 for 循环中移出来。修改后的代码看起来是这样的。

using System;

namespace Scenario1_2
{
    class Program
    { 
        static void Main(string[] args)
        {
           int counter, number, fact;

           Console.WriteLine("Please enter the number you wish to factorize");
           number = int.Parse(Console.ReadLine());
           fact = number;

           for (counter = number - 1; counter >= 1; counter--)
           {
               fact = fact * counter; 
           }
           Console.WriteLine("The number you entered was {0} and it's factorial is {1}", number, fact);
           Console.ReadLine();
        } 
    }
}

计算阶乘有很多方法。你也可以通过创建递归函数来完成。Google 可以在这些基础事项上帮助你很多。 谢谢!


0
int n = 4, fact = n;
for (int i = n; i > 1; i--)
{
    fact *= (i - 1);
}
Console.WriteLine(fact);
Console.ReadLine();

虽然这可能是问题的答案,但请花些时间解释为什么是这样。记住,未来的SO用户可能会找到这个答案,任何一点上下文都有帮助。 - Edson Menegatti

-1

为什么你把消息打印在循环内部?把它放到循环外面。

Console.WriteLine("The number you entered was {0} and it's factorial is {1}", number, fact);

-1
import java.util.Scanner;
public class Chapter5ProblemTwelve
{
   public static void main(String [] args)
   {
      Scanner keyboard = new Scanner(System.in);
      int number;
      int factor = 1;
      int counter;
      System.out.print("Enter a positive integer to display the factorial number: ");
      number = keyboard.nextInt();
      //If the number entered is less then zero. The program will tell the user to enter a positive number
      if (number <= 0)
      {
         System.out.println("Please enter a postive number and rerun the program again.");
      }
      else
      {
      // Math work preformed if user enters a postive number. Example if user enters 4.
      // 1*1 = 1, 1*2 = 2,1*3 = 3, 1*4 = 4, The program will multiple all the answers together 1*2*3*4 = 24
       for (counter = 1; counter <= number; counter++)
       {
         factor = factor * counter;
       }
       //display 
       System.out.println("The factorial number of " + number + " is: " + factor);  

      }

   }

}

1
输入一个非负整数以显示该整数的阶乘数字:4 4的阶乘数为:24 - shobbie14

-1
using System;
namespace factorial
{
    class Program
    {
        static void Main(string[] args)
        {
            int fact = 1; 
            Console.Write("Enter a number to find factorial:");
            int n = int.Parse(Console.ReadLine());
            for (int i = n; i > 0; i--)
            {
                fact = fact * i;
            }
            Console.Write("Factorial of" + n +"is :"+fact);
            Console.ReadLine();
        }
    }
}

你应该在你的答案中加入解释,这将有助于未来的其他人。[回答]。 - cosmoonot

-2

using System;

namespace septtwenty
{
    class Program
    {
        static void Main(string[] args)
        {
            int i, number, fact;
            System.Console.WriteLine("Enter the Number");
            number = int.Parse(Console.ReadLine());
            fact = number;
            for (i = number -1; i>=1; i--)
            {
                fact = fact * i;
            }
            System.Console.WriteLine("\nFactorial of Given Number is: "+fact);
            Console.ReadLine();
        }
    }
}


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接