import java.math.BigDecimal;
import java.util.Scanner;/**
 * @ClassName Test
 * @Description: TODO
 * @Author zxsilent
 * @Date 2019/9/21
 * @Version V1.0
 **/
public class Test {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入x:");
        double x = scanner.nextDouble();
        double taylor = Taylor(x);
        System.out.println(taylor);    }    //定义求阶乘的方法,递归
    public static int Factorial(int n) {
        if (n < 0) {
            return 0;
        } else if (n >= 0 & n <= 1) {
            return 1;
        } else {
            return n * Factorial(n - 1);
        }
    }    //保留小数点后六位小数
    public static double decimal(double num) {
        BigDecimal bd = new BigDecimal(num);
        BigDecimal bd1 = bd.setScale(6, bd.ROUND_HALF_UP);
        num = bd1.doubleValue();
        long ll = Double.doubleToLongBits(num);        return num;
    }    public static double Taylor(double x){
        int i,k;
        long fac=1;//阶乘
        double n=1,y=1,sum=1;
        for(i=2;(Math.abs(y))>=1e-6;i+=2)
        { n=n*(-1);
            for(k=1;k<=i;k++)
                fac=fac*k;
            y=Math.pow(x, i)*n/fac;
            sum+=y;
            fac=1;
        }
        return decimal(sum);
    }}

解决方案 »

  1.   

    /**
     * @ClassName Test03
     * @Description: TODO
     * @Author zxsilent
     * @Date 2019/9/21
     * @Version V1.0
     **/
    public class Test03 {    public static void main(String[] args) {
            System.out.println("e为:"+printE());
        }    public static double printE(){
            double e=1;
            int i=1;
           while ((1.0/Factorial(i))>=1e-4){
               e=e+1.0/Factorial(i);
               i++;
           }       return e;
        }
        //求阶乘
        public static int Factorial(int num){
            if (num>=0&&num<=1){
                return 1;
            }else {
                return num*Factorial(num-1);
            }
        }
    }
      

  2.   

    package com.example.demo.algorithm;public class CalculateE {    /**
         * @Author:
         * @Date: 2019/11/8 16:17
         * @Description:
         *
         * s=1+1/1!+1/2!+1/3!.....+1/n! 当1/n!小于0.0001时结束取值
         *
         */   public static Double getSum(){
          double s=1d;
          double n=1d;      while (1/getN(n)>=0.0001){
              s=s+(1/getN(n));
              n=n+1;
              System.out.println( "s: ---"+s );
          }
           s=s+1/getN(n);
           System.out.println( "s: ---"+s );
           return s;
       }
       /**
        * @Author:
        * @Date: 2019/11/8 16:31
        * @Description: 计算n!
        */
       public static double getN(double n){
           double s=n;
           while (n!=1){
               s=s*(n-1);
               n=n-1;
           }
           System.out.println(s);
           return s;
       }
        public static void main(String[] args) {
            System.out.println(getSum());
        }
    //主要是 数据类型要有浮点型}
      

  3.   

    因为在计算n!之前,(n-1)!已经计算出来了,可以在计算n!时加以利用,所以,不建议使用独立的阶乘函数,尤其是递归方式的阶乘函数,堆栈操作耗时耗系统资源。int n = 1;
    double jc = 1;
    double fjc = 1;
    double sum = 1;do {
    //利用之前的计算结果(n-1)!求n!
    jc *= n;
    fjc = 1 / jc;
    sum += fjc;
    n++;
    } while (fjc >= 0.0001);
    System.out.println("i=" + n + ", sum=" + sum);
    System.out.println(1+1+1/2+1/6);
      

  4.   

    第10题相较于第9题稍微复杂点,不过,基本思路是一样的,也就是尽量充分使用已有的计算结果。double x;
    Scanner sc = new Scanner(System.in);
    x = sc.nextDouble();// 计算步骤控制变量
    int n = 0;
    // x的2*次方
    double xn = x * x;
    // 2*n的阶乘
    double jc = 2;
    // 保存计算2*n的阶乘需要的临时量
    double tmp;
    // 精度控制,也就是下一步要增加的x^(2*n)/(2*n)!
    double c = xn / jc;
    // cos(x)
    double cos = 1;do {
    if (n % 2 == 0) {
    cos -= c;
    } else {
    cos += c;
    }
    n++;
    // 计算2*n的阶乘
    tmp = 2 * (n + 1);
    jc *= tmp;
    jc *= (tmp - 1);
    // 计算x的2*n次方
    xn *= (x * x);
    // 计算精度控制量
    c = xn / jc;
    } while (c >= 0.000001);
    System.out.println("i=" + n + ", cos(" + x + ")=" + cos);
    sc.close();