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; } } }
原来是学生啊...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; }
你用的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(); }
Sorry, 掉了个等号 for (int i = 1; i <= num; i++)
阶乘容易超过int的范围,建议用大数乘法做。。
呵呵 经过改正 用十楼的 结果终于出来了 我汗 半天我还在用我们原来定义好的 例如useing system 所以出不来 换了就出来了
递归调用 public long getResult(int num) { //首先判断递归结束的条件 if(num==1) { return 1; } //每次再调用一下该方法,得到返回值。 long result=getResult(num-1)*num; return result; }就这么简单了吧~~
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); } 这个是老师指导下做出来得, 虽然只能求一些小得数据,不过对于我们这些初学者来说已经可以了。 半天这么容易, 看来程序得写作需要仔细.严谨得考虑啊。
public int getResult(int i)
{
if(i > 1)
{
return i* getResult(i-1);
}
return 1;
}
MessageBox.Show(getResult(100).ToString());
如果数字太的话会溢出,这里100的话用double,再大就long
{
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;
}
}
}
{
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;
}
哎换了个来个这 我们用的主题是 static void Main(string[] args); 是这个 就学了个这个...
http://confach.cnblogs.com/archive/2005/07/14/192703.aspx
这里改一下:
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();
}
Sorry, 掉了个等号
for (int i = 1; i <= num; i++)
public long getResult(int num)
{
//首先判断递归结束的条件
if(num==1)
{
return 1;
}
//每次再调用一下该方法,得到返回值。
long result=getResult(num-1)*num;
return result;
}就这么简单了吧~~
{
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);
}
这个是老师指导下做出来得,
虽然只能求一些小得数据,不过对于我们这些初学者来说已经可以了。
半天这么容易,
看来程序得写作需要仔细.严谨得考虑啊。