分別使用forwhiledo?
//計(jì)算階乘的私有方法
private int jiecheng(int i){
if(i==1)
return 1;
else
return i*jiecheng(i--);
}
//for
private int sumjiechengByFor(int n)
{
int sum =0;
for(int i=1;i
{
sum +=jiecheng(i);
}
return sum;
}
//while
private int sumjiechengByWhile(int n)
{
int sum =0,i=1;
while(i
{
sum +=jiecheng(i);
i++;
}
return sum;
}
//dowhile
private int sumjiechengByDoWhile(int n)
{
int sum =0,i=1;
if(i
do
{
sum +=jiecheng(i);
i++;
}while(i
return sum;
}