使用while语句,计算整数n的阶乘
N的阶乘计算表示 n!=n*(n-1)*(n-2)…2*1
N的阶乘计算表示 n!=n*(n-1)*(n-2)…2*1
解决方案 »
- eclipse3.1 complier compliance level 没有6.0和1.6
- java实现获取所有的包以及下面所有的类存入mysql
- 我的QQ栏内显示QQ图标是以JList实现的 可在JList内不能显示JIF图片,我编段动画代码进去,图标还是不能动,总之无法实现QQ图标跳动 那位大虾帮帮
- windows98 或me平台下可以安装java142吗
- 关于<java rules>中的一处不明白
- 小问题!~
- 大家帮我看个程序,哪儿出错了?(在线等待)
- 数组问题(请帮我详细分析一下)
- tomcat问题,在线等待
- 这段代码哪儿有错?
- 怎么无法调动WINDOW_DESTROY?
- 文件上传和接收的思路
public static void main(String[] args) throws IOException{
System.out.println("请输入一个数:");
int one=System.in.read();
int sum=1;
for(int i=one;i>0;i--){
sum*=i;
}
System.out.println(one+"!"+"="+sum);
}
}
public class Test1 {
public static void main(String[] args) {
System.out.println(caculate(22));
System.out.println(caculate2(20));
}
/**
* n!(当n>20时采用此方法)
* @param n
* @return
*/
public static String caculate(int n) {
if (n < 0) {
System.out.println("n必需大于0");
return "error";
} else if (n == 0 || n == 1) {
return "1";
} else {
//BigInteger multiply(BigInteger val) 返回其值为 (this * val) 的 BigInteger。
return BigInteger.valueOf(n).multiply(new BigInteger(caculate(n-1))).toString();
}
}
/**
* n!(当n<=20时采用此方法)
* @param n
* @return
*/
public static long caculate2(long n) {
if (n < 0) {
return 0;
} else if (n == 0 || n == 1) {
return 1;
} else {
return n * caculate2(n -1);
}
} }
public class Test { public static int fact(int n)
{
while (n!=0)
{
return n*fact(n-1);
}
return 1;
}
public static void main(String args[])
{
System.out.println(fact(3));
}
}
public static void main(String args[])
{
int n=5,i=1,z=1,k=1;
while(k<=n)
{
z*=k;
k++;
}
System.out.println("n!="+z);
}
}不好意思啊
刚才看错了
给你发错了
这个绝对够简单
够明了
import java.util.*;public class TestBigIntegerClass {
public static void main(String[] args) {
BigInteger value , result , sum;
int m;
sum = BigInteger.valueOf(0);
result = BigInteger.valueOf(1);
Scanner in = new Scanner(System.in);
System.out.println("取n值:");
m = in.nextInt();
for(long i=1;i<=m;i++) {
value = BigInteger.valueOf(i);
result = result.multiply(value);
//sum =sum.add(result);
}
System.out.println("n的阶乘为: "+ result);
}}
if(n == 0 || n == 1)
return 1;
else
return n * fact(n - 1);
}
这样也行呀...个人觉得挺方便的..hehe
{
while (!n.equals(BigInteger.ZERO))
{
return n.multiply(fact(n.subtract(BigInteger.valueOf(1))));
}
return BigInteger.valueOf(1);
}
public static void main(String args[])
{
BigInteger b=BigInteger.valueOf(100);
System.out.println(fact(b));
}
}
if(n > 0)
{
while(n)
{
m *= n;
--n;
}
}
else
printf("input error\n");
if(n >= 0)
{
int temp = n;
while(n)
{
m *= n;
--n;
}
printf("%d! = %d\n",temp,m);
}
else
printf("input error\n");