计算一个数的因子

时间:2016-06-30 03:44:20

标签: 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 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();
            }
        } 
    }
}

8 个答案:

答案 0 :(得分: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;
        } 
    }
}  

任何地方都没有循环,函数calls itself

你也可以这样做:

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 :(得分:1)

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

答案 2 :(得分: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可以帮助您解决这些基本问题。 谢谢!

答案 3 :(得分:0)

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

答案 4 :(得分:-1)

为什么要在循环中打印消息。将它放在循环外面

uninitialized constant Model (NameError)

答案 5 :(得分:-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();
        }
    }
}

答案 6 :(得分:-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);  

      }

   }

}

答案 7 :(得分:-2)

&#13;
&#13;
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();
        }
    }
}
&#13;
&#13;
&#13;