如何用java实现数列求和?答案如下
package nameyu;public class Test { public static void main(String[] args) { // TODO Auto-generated method stub double sum=0; for(int i=1;(2*i-1)<=97;i++){ double a=((2*i)*1.0-1)/(1+2*i); sum+=a;// System.out.println(a); } System.out.println(sum); }}计算π值
π= 3.141592653589793238462643383279502我也只能大概记住这么多位。输入10000 -100000自己输出看看就行。package nameyu;import java.util.Scanner;public class Test { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input=new Scanner(System.in); System.out.print("Enter a number:"); int n=input.nextInt(); double sum=0; for(int i=1;i<=n;i++){ sum+=(double)(Math.pow((-1),(i+1))/(2*i-1)); } System.out.println((double)(4*sum)); }} /*Enter a number:100003.1414926535900345Enter a number:200003.1415426535898248Enter a number:300003.141559320256462*/
页:
[1]