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:10000 3.1414926535900345 Enter a number:20000 3.1415426535898248 Enter a number:30000 3.141559320256462 */
|