2011-09-03 126 views
0

我正在嘗試編寫一個程序,該程序將獲取用戶輸入值,並詢問他們是否要計算1到n的數字值或n的階乘值! 這是我迄今爲止在C#中計算階乘因子

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

namespace Project_2_Part_B 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     {     
      var Fkeylow = "f"; 
      var FkeyCap = "F"; 
      var Skeylow="s"; 
      var SkeyCap="S"; 
      int n = 0; 
      long factorial = n; 

      Console.WriteLine("Input a value n"); 
      n = Convert.ToInt32(Console.ReadLine()); 

      Console.WriteLine("Do you want to calculate factorial or sum"); 
      Console.WriteLine("Enter F or S"); 

      var A = Console.ReadLine(); 

      if (A == Fkeylow) 
       Console.WriteLine(); 

      if (A == FkeyCap) 
       Console.WriteLine(); 

      var B=Console.ReadLine(); 

      if (B == Skeylow) 
       Console.WriteLine(); 

      if (B == SkeyCap) 
       Console.WriteLine(); 

      Console.WriteLine("Press any key to close..."); 
      Console.ReadLine(); 
     } 
    } 
} 

我的問題是用計算的語法,以使代碼執行N *(N-1),而N> 1 任何幫助將不勝感激。先進的謝謝!

+0

HTTP://en.wikipedia。 org/wiki/Factorial –

+0

@Stephen你的意思是:input = 10 - > print 10!那麼9!那麼...... 1! ? – 2011-09-03 04:58:26

+0

像用戶輸入5然後按「f」因數輸出應該是120.(5x4x3x2x1) – Thomas

回答

1
static void Main(string[] args) 
{ 
Console.WriteLine("Input a value n"); 
string number = Console.ReadLine(); // read number 
int n = Convert.ToInt32(number); // converting to int 

Console.WriteLine("Do you want to calculate factorial or sum"); 
Console.WriteLine("Enter F or S"); 
string choose = Console.ReadLine(); // F or S 

int result = -1; // to view later 

if (choose == "f" || choose == "F") 
{ 
result = 1; 
for (int i = n; i >= 1; i--) // loop for calculating factorial 
    result *= i; 
} 
else if (choose == "s" || choose == "S") 
{ 
result = 0; 
for (int i = n; i >= 1; i--) // loop for calculating sum 
    result += i; 
} 

Console.WriteLine(result); // printing answer 

Console.WriteLine("Press any key to close..."); 
Console.ReadLine(); 
} 

如果答案對您有幫助,請不要忘記標記爲最佳答案。

+0

我總是過於複雜的事情。非常感謝你的幫助! – Thomas

0

使用遞歸是做它的另一種方式:

static void Main(string[] args) 
{ 
     int n = 5; 
     Console.WriteLine(factorial(n)); 
     Console.WriteLine(sum(n)); 
     Console.Read(); 
} 

public static int sum(int n) 
{ 
    if(n==0) 
     return 0; 
    return n+sum(n-1); 
} 
public static int factorial(int n) 
{ 
    if (n == 1) 
     return 1; 
    return n * factorial(n - 1); 
} 

打印:

120 -->5! 
15 -->1+2+3+4+5 
+0

@lcarus他看起來像begginer,不能理解遞歸解決方案 – 2011-09-03 05:12:54

+0

@DDD你去了,他不喜歡遞歸的版本。真是太遺憾了:'( – Icarus

+0

我很確定「Project_2_Part_B」是作爲初學者的免費贈品;) –

0

,基本解決方案是在這裏,測試,

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("Factorial of six is :" + manipulator.factorial(16)); 
      Console.ReadLine(); 
     } 
    } 
class NumberManipulator 
{ 
    public int factorial(int num) 
    { 
     int result=1; 
     int b = 1; 
     do 
     { 
      result = result * b;//fact has the value 1 as constant and fact into b will be save in fact to multiply again. 
      Console.WriteLine(result); 
      b++; 
     } while (num >= b); 
     return result; 
    } 
    } 
}