菜鸟涂鸦作品展_No.22

//【程序22】 题目:利用递归方法求200!。
import java.util.*;
class shuju{
double h;//设置成double将能进行更大数据的运算,至少比text21中的计算能力要大
shuju(double i){
h=i;
}
}
public class _22_200jiecheng {
public static void jiecheng(Vector<shuju> n,int a){
double num=0;
for (int j=0 ; j<n.size(); j++){
n.elementAt(j).h=n.elementAt(j).h*a;
}
for (int j=0; j<n.size(); j++){
if ((n.elementAt(j).h+num)>=10){
try
{
num=(n.elementAt(j).h-n.elementAt(j).h%10)/10;
n.elementAt(j+1).h=n.elementAt(j+1).h+num;
n.elementAt(j).h=n.elementAt(j).h%10;
}
catch(ArrayIndexOutOfBoundsException aiobe)
{
num=(n.elementAt(j).h-n.elementAt(j).h%10)/10;
n.addElement(new shuju(num));
n.elementAt(j).h=n.elementAt(j).h%10;
}
}
}
}
public static void main(String[] args) throws Exception
{
_22_200jiecheng jc=new _22_200jiecheng();
Vector<shuju> n=new Vector<shuju>();
shuju m=new shuju(1);
n.addElement(m);
// n.addElement(1);
for (int i=1 ; i<=200 ; i++){
jc.jiecheng(n,i);
}
for (int i=n.size() ; i>0; i--){
System.out.print((int)n.elementAt(i-1).h);
}
}
}

涂鸦心得:

  这个程序前后纠结了两天,花的时间也不少,算是有两个收获:

  1.Vector动态数组的使用,这个能大能小的容量以后用的机会可能较大。但是这个指向的对象的值竟然不能直接改让人很纠结,只有新建一个类,将其指向的元素转换为一个变量,才能重新赋值,也是程序中出现众多.h的原因。

  2.try-catch的使用,以前看见有程序用到try-catch就感觉很牛逼,现在自己用了用,还是挺好用的。面前所知道的就是出现异常则执行一种情况,无异常则执行另一种情况。有空了再认真学习一下。

  本周还有8个程序呢,坚持。。。

原文地址:https://www.cnblogs.com/aniuer/p/2670695.html