日期:2014-05-20  浏览次数:20828 次

关于for的循环语句
import java.util.Scanner;
import java.math.BigInteger;
public class T5
{
public static void main(String[] args)
{
int a;
int b=1;
System.out.println("请输入要求阶乘的数:");
Scanner sc=new Scanner(System.in);
a=sc.nextInt();
BigInteger d=BigInteger.valueOf(1);
do
{
d=d.multiply(BigInteger.valueOf(b));
b++;
}while(b<=a);
System.out.println("所求的数的阶乘是:"+d);
}
}
求阶乘问题用do-while和while语句会用,但是用for的话该怎么写?我是卡在for(a;b;c)这里不会,我知道a是初始语句,b是跳出循环的条件语句,c是增量语句,但初始的赋值的语句多了怎么办?就这里不懂
java

------解决方案--------------------
BigInteger d = BigInteger.valueOf(1);
for(;b <= a; b ++ )
{
d = d.multiply(BigInteger.valueOf(b));
}

System.out.println("所求的数的阶乘是:" + d);

------解决方案--------------------
http://www.cnblogs.com/birdshover/archive/2008/08/04/1260499.html
一般这种问题能百度谷歌解决尽量自己解决,像这些博文一般讲的比较全,也有例子,LZ可以看看
------解决方案--------------------
for(int b=1;b<=a;b++){

}
------解决方案--------------------
引用:
BigInteger d = BigInteger.valueOf(1);
for(;b <= a; b ++ )
{
d = d.multiply(BigInteger.valueOf(b));
}

System.out.println("所求的数的阶乘是:" + d);

------解决方案--------------------
第一个分号前没有语句,也就是没有初始语句。直接写分号是告诉系统,b<=a是判断条件