各位高高手大家好,我是一个菜鸟,正在学习C#,现在有个问题,就是怎样在VS2005的控制台应用程序上做出一个最基本的,可以求出一个数字的阶乘的,希望哪个老鸟指点下迷津,小弟不胜感激涕零....

解决方案 »

  1.   


    public   int   getResult(int   i)   
      {         
              if(i   >   1)   
              {   
                    return   i*   getResult(i-1);   
                }   
              return   1;                     
      }
    MessageBox.Show(getResult(100).ToString()); 
    如果数字太的话会溢出,这里100的话用double,再大就long
      

  2.   

    ..............首先谢谢这位高高手大大的热心回复,毕竟没分数....不过我们现在只学习了循环语句 就是只有while 就只用这个叫求出.....没办法 继续提问吧.......
      

  3.   

    using System;namespace ConsoleApplication1
    {
        class Program
        {
            // The maximum of the number, you may set this number as you want.
            const int maxNumber = 10;         static void Main(string[] args)
            {
                Console.Write("Please input the number:");
                string inputString = Console.ReadLine();            int result = 1;
                int inputValue = 0;            try
                {
                    // Try to parse the input string to a number, 
                    // if not successed, output the error message
                    if (int.TryParse(inputString, out inputValue))
                    {
                        result = Factorial(inputValue);
                        Console.WriteLine("The factorial of {0} is {1}.", inputValue, result);
                    }
                    else
                    {
                        Console.WriteLine("Your input is not a valid number.");
                    }
                }
                catch (ArgumentOutOfRangeException)
                {
                    Console.WriteLine("Sorry, your input value {0} is out of range, it should between 0 and {1}", inputValue, maxNumber);
                }
                catch (OverflowException)
                {
                    Console.WriteLine("Sorry, the calculation of the input value cause an over flow");
                }
            }        // The method to get the factorial
            static int Factorial(int num)
            {
                // Check for the input number range.
                if (num < 0 || num > maxNumber)
                {
                    throw new ArgumentOutOfRangeException();
                }            if (num == 0)
                    return 1;            int result = 1;            for (int i = 1; i < num; i++)
                {
                    result *= i;
                    // When the result is too large, it will cause over flow
                    // we check here the result, if it is less than zero, the 
                    // over flow happen.
                    if (result < 0)
                        throw new OverflowException();
                }            return result;
            }
        }
    }
      

  4.   

    原来是学生啊...public   int   getResult(int   i)   
      {   
          int result=1; 
          for(int j=1;j<=i;j++)
          {
              result*=j;      
          }
          return result;                    
      }或者:public   int   getResult(int   i)   
      {   
          int result=1,j=2; 
          while(j<=i)
          {
              result*=j; 
              j++;    
          }
          return result;                    
      }
      

  5.   

    自己写一个吧,要考虑溢出问题。不要直接用int,long什么的,那个毕竟还是有溢出的。考虑用stringbuilder。这个无所谓了。
      

  6.   

    错误 1 “_013830.Program.Main(string[])”必须声明主体,因为它未标记为 abstract 或 extern D:\My Documents\Visual Studio 2005\Projects\1013830\1013830\Program.cs 10 21 1013830
    哎换了个来个这 我们用的主题是 static void Main(string[] args); 是这个 就学了个这个...
      

  7.   

    Google下大数阶乘 http://blog.csdn.net/liangbch/category/292924.aspx 
    http://confach.cnblogs.com/archive/2005/07/14/192703.aspx
      

  8.   

    你用的10楼的程序吧?
    这里改一下:
                for (int i = 1; i <= num; i++) 
                { 
                    result *= i; 
                    // When the result is too large, it will cause over flow 
                    // we check here the result, if it is less than zero, the 
                    // over flow happen. 
                    if (result < 0) 
                        throw new OverflowException(); 
                } 
      

  9.   


    Sorry, 掉了个等号
    for (int i = 1; i <= num; i++)
      

  10.   

    阶乘容易超过int的范围,建议用大数乘法做。。
      

  11.   

    呵呵 经过改正 用十楼的 结果终于出来了 我汗 半天我还在用我们原来定义好的 例如useing system 所以出不来 换了就出来了
      

  12.   

    咦 不对啊 这个也是 useing system 看来是原来运行时程序是错误的 现在改后就好了 谢谢几位大大了  我一定女里学习 报效祖国 哇哈哈
      

  13.   

    递归调用
    public long getResult(int num)
    {
      //首先判断递归结束的条件
      if(num==1)
      {
        return 1;
      }
      //每次再调用一下该方法,得到返回值。
      long result=getResult(num-1)*num;
      return result;
    }就这么简单了吧~~
      

  14.   

     while (true)
                {
                    Console.WriteLine("输入一个数据:");
                    string a = Console.ReadLine();
                    int a1 = int.Parse(a);
                    int i = 1;
                    int s = 1;
                    while (i <= a1)
                    {
                        s = s * i;
                        i++;
                    }
                    Console.WriteLine(s);
                }
    这个是老师指导下做出来得,
    虽然只能求一些小得数据,不过对于我们这些初学者来说已经可以了。
     半天这么容易,
     看来程序得写作需要仔细.严谨得考虑啊。