C# 计算一个数的阶乘

C# 计算一个数的阶乘,c#,factorial,C#,Factorial,当我输入数字6来计算其阶乘时,它返回30(这是错误的) 为什么我的程序产生不正确的输出 using System; namespace Scenario1_2 { class Program { static void Main(string[] args) { int counter, number, fact; Console.WriteLine("Please enter the numb

当我输入数字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();
            }
        } 
    }
}

程序暂停,等待输入。您需要将第二个
控制台.ReadLine()
移出循环。很可能是
控制台.WriteLine()
,除非您希望看到每个迭代都完成

为什么要在循环内打印消息。请将其置于循环外

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

您需要从
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();
        } 
    }
}
有很多方法可以计算阶乘。您还可以通过创建递归函数来实现这一点。谷歌可以在这些基本的事情上帮你很多。
谢谢

你对编程还是个新手,或者至少是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++;
        } 
    }
}
使用系统;
命名空间场景1\u 2
{
班级计划
{ 
静态void Main(字符串[]参数)
{   
Console.WriteLine(“请输入您希望分解的数字”);
int number=int.Parse(Console.ReadLine());
WriteLine(“您输入的数字是{0},它的阶乘是{1}”),number,Factorials().Skip(number-1.First());
Console.ReadKey(true);
}
静态IEnumerable阶乘()
{
int n=1,f=1;
而(真)收益率f=f*n++;
} 
}
}
使用系统;
名称空间9月20日
{
班级计划
{
静态void Main(字符串[]参数)
{
int i,数字,事实;
System.Console.WriteLine(“输入号码”);
number=int.Parse(Console.ReadLine());
事实=数字;
对于(i=number-1;i>=1;i--)
{
事实=事实*i;
}
System.Console.WriteLine(“\n给定数字的工厂为:“+fact”);
Console.ReadLine();
}
}
}
导入java.util.Scanner;
公共课第五章问题十二
{
公共静态void main(字符串[]args)
{
扫描仪键盘=新扫描仪(System.in);
整数;
整数因子=1;
整数计数器;
System.out.print(“输入一个正整数以显示阶乘数:”);
number=键盘.nextInt();
//如果输入的数字小于零,程序将告诉用户输入正数

如果(数字将你的控制台移动到循环外。writeline和readline。或者继续按enter键直到你得到答案。哦,我明白了!非常感谢你,这很有意义!试试哇,非常感谢你的精彩示例!研究它们给了我一些重要的见解。它实际上也帮助我更好地理解递归!真是太感谢你了!!谢谢uld在您的答案中添加解释,这将有助于将来的其他人。虽然这可能是问题的答案,但也需要一些时间来解释原因。记住,so的未来用户可能会发现这一点以及任何上下文帮助。输入非负整数以显示整数的阶乘数:4的阶乘数4 is:24
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();
        }
    }
}
int n = 4, fact = n;
for (int i = n; i > 1; i--)
{
    fact *= (i - 1);
}
Console.WriteLine(fact);
Console.ReadLine();
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);  

      }

   }

}