BigDecimal类型数字可用来做超大的浮点数的运算
该类对象的add()方法做加法运算;multiply()方法做乘法运算
calculate 1+1/2!+1/3!+...+1/10!
import java.math.BigDecimal;
import java.util.Scanner;
public class Triangle {
public static void main(String[] args){
Scanner scan=new Scanner(System.in);//创建扫描器
BigDecimal sum=new BigDecimal(0.0);
BigDecimal factorial=new BigDecimal(1.0);
int i=1;
while(i<=10){
sum=sum.add(factorial);
++i;
factorial=factorial.multiply(new BigDecimal(1.0/i));//计算阶乘项
}
System.out.println("1+1/2!+...+1/10!="+sum);
}
}