2013-03-04 219 views
3

这是我到目前为止有:计算阶乘

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); 
      } 

    } 
    } 
    } 

现在它只是给我的数字的平方,他们不阶乘。我如何使它重复次数作为输入,以产生阶乘?

而且我不知道是否有必要限制程序唯一的非负整数,但如果我想这部分刚刚结束程序就在那里,而不是循环回到开始的。

回答

0

你的循环分配在一个循环的数量结果的平方,并退出的时候了。您需要对其进行更改,以便将结果反复乘以1到N之间的数字。

分配1到阶乘,并通过计数器在矿井循环乘以:

factorial *= counter; 

不要忘记在1

0

开始你的柜台,我会给你暗示。

  1. 将变量初始化为1.假设它为Answer
  2. 从1运行一个循环数做的Answer = Answer * loopIterationVariable操作。每次迭代之后,循环增量循环迭代变量由1
6

的没有任何意义可言!如果你正在寻找一个因子,那么你必须从一个乘以所有号码为给定数。那将是:

int factorial = 1; 

for (counter = 1; counter <= number; counter++) 
{ 
    factorial = factorial * counter; 

} 

Console.WriteLine("The factorial of {0} is {1}", number, factorial); 

这将计算一个数的阶乘。你将不得不重复你想要的所有数字。

0
static void Main(string[] args) 
    { 

     Console.WriteLine("Enter an integer number to factorise"); 

     int ans = 1; 
     int a = int.Parse(Console.ReadLine()); 

     for (int i = 1; i <= a; i++) 
     { 
      ans = ans * i; 
     } 
     Console.WriteLine(ans.ToString()); 
     Console.ReadLine();    
    } 
+0

单刀直入:)没有多余的话 – 2013-09-30 15:03:07

1
 string str = Interaction.InputBox("Enter the number to find the factorial of: "); 
     double counter = double.Parse(str); 
     long factorial = 1; 

     while (counter > 1) 
     { 
      factorial *= (long)counter--;//factorial = factorial * counter - 1 
     } 
     MessageBox.Show("The factorial of " + str + " is " + String.Format("{0:N}", factorial)); 

我用于Interaction.InputBox()方法

0

递归实现一个Microsoft.VisualBasic参考以及基本实现如下

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace ConsoleApplication50 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 

     NumberManipulator manipulator = new NumberManipulator(); 
     Console.WriteLine("Please Enter Factorial Number:"); 
     int a= Convert.ToInt32(Console.ReadLine()); 

     Console.WriteLine("---Basic Calling--"); 
     Console.WriteLine("Factorial of {0} is: {1}" ,a, manipulator.factorial(a)); 

     Console.WriteLine("--Recursively Calling--"); 
     Console.WriteLine("Factorial of {0} is: {1}", a, manipulator.recursively(a)); 

     Console.ReadLine(); 
    } 
} 

class NumberManipulator 
{ 
    public int factorial(int num) 
    { 
     int result=1; 
     int b = 1; 
     do 
     { 
      result = result * b; 
      Console.WriteLine(result); 
      b++; 
     } while (num >= b); 
     return result; 
    } 

    public int recursively(int num) 
    { 
     if (num <= 1) 
     { 
      return 1; 
     } 
     else 
     { 
      return recursively(num - 1) * num; 
     } 
    } 
    } 
} 
0
private void btnCalculate_Click(object sender, EventArgs e) 
    { 
     try 
     { 
      userinputF = Int32.Parse(txtFaculteit.Text); 
      for (counter = 1; counter <= userinputF; counter++) 
      { 
       answer = answer *= counter; 
      }   
     } 
     catch (Exception exception) 
     { 

       MessageBox.Show("Please fill in a number " + exception.Message); 
     } 

     lblAnswerFaculteit.Text = lblAnswerFaculteit.Text + "The faculty of " + userinputF + " = " + answer; 
    } 
} 

}

0
int userinputF; 
    int counter; 
    int answer =1; 


    public Form1() 

    { 
     InitializeComponent(); 

    } 

    private void btnCalculate_Click(object sender, EventArgs e) 
    { 
     try 
     { 
      userinputF = Int32.Parse(txtFaculteit.Text); 
      for (counter = 1; counter <= userinputF; counter++) 
      { 
       answer = answer *= counter; 
      }   
     } 
     catch (Exception exception) 
     { 

       MessageBox.Show("Please fill in a number " + exception.Message); 
     } 

     lblAnswerFaculteit.Text = lblAnswerFaculteit.Text + "The faculty of " + userinputF + " = " + answer; 
    } 
} 

}

+0

请提供一些更多的解释,您的文章 – rbr94 2016-10-20 08:56:35