package ch22;

public class CalculateDemo {

public static void main(String[] args) {
int n=100;// 循环上限
double sum=0;
for(double i=1;i<=n;i++){
if(i%2==0)
sum=sum-(1/i);//如果i为偶数,则减
else
sum=sum+(1/i);//如果i为奇数,则加
}
System.out.println("sum="+sum);
}

}

求 1-1/2+1/3-1/4+···1/99-1/100。_i++