java中的分数系列

https://img1.sycdn.imooc.com/6597a5510001476616770451.jpg

我需要使用循环来查找以下系列的总和:


(2/3)-(4/5)+(6/7)-(8/9)+......±n

我必须仅在该程序中使用 for 循环。参考代码看看我做了什么:


import java.util.Scanner;

public class P64 {

    public static void main(String args[]) {

        Scanner sc=new Scanner(System.in);

        System.out.println("Enter the limit");

        double n=sc.nextDouble();

        double sum=0;

        for(double i=1;i<=n;i++) {

            if(i%2==0)

            sum=sum-(++i/i++);

            else

            sum=sum+(++i/i++);

        }

            System.out.println(sum);

    }

}

我已经尝试过,但输出不是 1 就是 0。


qq_遁去的一_1
浏览 58回答 4
4回答

守着星空守着你

您应该使用单独的变量作为值,就像在循环中使用相同的变量一样,您的系列会让它变得复杂,所以,试试这个:public static void main(String[] args){&nbsp; &nbsp; Scanner sc=new Scanner(System.in);&nbsp; &nbsp; System.out.println("Enter the limit");&nbsp; &nbsp; double n=sc.nextDouble();&nbsp; &nbsp; double sum=0;&nbsp; &nbsp; double j=1;&nbsp; &nbsp; for(double i=1;i<=n;i++)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if(i%2==0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sum=sum-(++j/++j);&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sum=sum+(++j/++j);&nbsp; &nbsp; }&nbsp; &nbsp; System.out.println(sum);}Input: 4Output: -0.16507936507936516

慕姐4208626

我删除了预增量/后增量的技巧,并让限制决定了相加的项数。import java.util.Scanner;public class P64{&nbsp; &nbsp; public static void main(String args[])&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Scanner sc=new Scanner(System.in);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Enter the limit");&nbsp; &nbsp; &nbsp; &nbsp; double n=sc.nextDouble();&nbsp; &nbsp; &nbsp; &nbsp; double sum=0;&nbsp; &nbsp; &nbsp; &nbsp; for(double i=1;i<=n;i++)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double delta = (2*i)/(2*i+1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(i%2==0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sum -= delta;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sum += delta;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(sum);&nbsp; &nbsp; }}

慕婉清6462132

我将使用一个变量进行交替+-,并在每次迭代时采取两步:public static void main(String args[]) {&nbsp; &nbsp; Scanner sc = new Scanner(System.in);&nbsp; &nbsp; System.out.println("Enter the limit");&nbsp; &nbsp; double n = sc.nextDouble();&nbsp; &nbsp; double sum = 0;&nbsp; &nbsp; int sign = 1;&nbsp; &nbsp; for (double i = 2; i <= n; i = i+2 ) {&nbsp; &nbsp; &nbsp; &nbsp; sum = sum + (sign * (i/(i+1)));&nbsp; &nbsp; &nbsp; &nbsp; sign = -sign;&nbsp; &nbsp; }&nbsp; &nbsp; System.out.println(sum);}

MMTTMM

我认为越简单越好。将其封装在函数中可以使您的代码更易于在主方法之外进行测试和使用。/**&nbsp;* @link https://stackoverflow.com/questions/58606895/series-with-fractions-in-java&nbsp;*/public class P64 {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; int n = (args.length > 0) ? Integer.parseInt(args[0]) : 10;&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(String.format("n: %5d sum: %10.5f", n, series(n)));&nbsp; &nbsp; }&nbsp; &nbsp; static double series(int n) {&nbsp; &nbsp; &nbsp; &nbsp; int sign = 1;&nbsp; &nbsp; &nbsp; &nbsp; double sum = 0.0;&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 1; i <= n; ++i) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double x = 2.0*i;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; double term = sign*x/(x+1.0);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sum += term;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sign *= -1;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return sum;&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java