double result = 0;
double temp = 0;
for (int i=0; i<10000; i++) {
temp = (i%2==0?1:-1) / (i*2+1);
result += temp;
}
System.out.println("结果为" + result);
这是核心代码。希望能帮到你
public class feishu {
public static void main(String args[]){
double a=-1;
double b,c;
double sum=0;
for(c=1;c<=10000;c++){
a=a+2;
if(c%2==0)
{b=(-1)*(1/a);
sum=sum+b;}
else if(c%2!=0)
{b=1/a;
sum=sum+b;}
}System.out.println(sum);
}
}
public class Sum {
static double sum=1;
public static double result(int i){
for(int x=1;x<=i-1;x++)
sum+=(double)Math.pow(-1, x)/(2*x+1);
return sum;
}
public static void main(String args[]){
System.out.println(result(10000));
}
}
double sum=0.0;
double sign=1.0;
for(int i=0;i<10000;i++,sign *=-1)
sum +=sign/(2*i+1)
System.out.println("结果是:"+sum);