【问题标题】:Series generation系列生成
【发布时间】:2015-05-01 16:51:14
【问题描述】:

我正在尝试找出系列的总和,1/2! - 2/3! + 3/4! - 4/5! ... 名词。抱歉,如果这听起来很尴尬,但总和始终显示为 0.0。我不知道发生了什么,我才刚刚开始。谁能指出错误并建议如何解决?谢谢!

import java.util.Scanner;
public class Series {
    /*
     * Series design: 1/2! - 2/3! + 3/4! - 4/5! .. n 
    */
    static double sum = 0; static int n;
    Scanner sc = new Scanner(System.in);
    public static int fact(int n){
        int fact = 1; 
        for (int i = 1; i<=n; i++){
            fact *= i;
        }
        return fact;
    }
    void generate(){
        double sign = 1.0; double term;
        for (int i = 1; i<=n; i++){
            term = i/fact(i+1) * sign;
            sum += term;
            sign *= -1;
        }
    }
    void accept(){
        System.out.println("Enter the value of n:");
        n = sc.nextInt(); 
    }
    public static void main(String[] args){
        Series o = new Series();
        o.accept();
        o.generate();
        System.out.println("The sum of the series is: " +sum);
    }
}

【问题讨论】:

  • 整数除法.....

标签: java series factorial


【解决方案1】:

您的问题是 i/fact(i+1) 是一个 int 除法,所以它被截断为 0(因为它小于 1)。

将其更改为(double)i/fact(i+1)

或者,你可以写

term = sign*i/fact(i+1);

因为sign 已经是双倍的,所以它会确保sign*i 也是双倍的,并且除法将是一个浮点除法。

【讨论】:

    【解决方案2】:

    ifact(i+1) 都是ints,所以你正在执行integer division。由于i &lt; fact(i+1),每个这样的术语都会产生一个零。

    您将sign 定义为double 的想法是正确的,但是由于/* 具有相同的优先级,因此您首先执行整数除法,然后再乘以双倍@987654330 @。将它移到表达式的开头应该可以解决问题:

    void generate(){
        double sign = 1.0; double term;
        for (int i = 1; i<=n; i++){
            term = (sign * i) / fact(i+1);
            sum += term;
            sign *= -1;
        }
    }
    

    【讨论】:

      【解决方案3】:
      import java.util.Scanner;
      
      public class Series {
      
      
          static double sum = 0.0;
          static int n=0;
          static Scanner sc = new Scanner(System.in);
      
          public static int fact(int n)
          {
             int fact = 1; 
      
            for(int i = 1; i<=n; i++){
      
              fact *= i;
          }
          return fact;
         }
      
          static void generate(){
           //because of static property
      
           double sign = 1.0; double term;
           for(int i = 1; i<=n; i++){
              term = (i* sign)/fact(i+1) ;
              sum += term;
              sign *= -1;
           }
      }
           static void  accept(){
      
             //because of static property
      
             System.out.println("Enter the value of n:");
             n = sc.nextInt(); 
          }
          public static void main(String[] args){
      
          Series.accept();
          Series.generate();
          System.out.println("The sum of the series is: " +sum);
      }
      }
      

      【讨论】:

        猜你喜欢
        • 2011-10-31
        • 1970-01-01
        • 1970-01-01
        • 2021-04-17
        • 2011-05-27
        • 1970-01-01
        • 1970-01-01
        • 2018-05-19
        • 1970-01-01
        相关资源
        最近更新 更多