用sum += 1.0 / i * j 来求和;
j = -j 来改变正负;
public class TestDemo2 {
public static void main(String[] args) {
int i;
int j = 1;
double sum = 0;
for(i=1;i<101;i++){
sum += 1.0/i * j;
j = -j;
}
System.out.println(sum);
}
}
用sum += 1.0 / i * j 来求和;
j = -j 来改变正负;
public class TestDemo2 {
public static void main(String[] args) {
int i;
int j = 1;
double sum = 0;
for(i=1;i<101;i++){
sum += 1.0/i * j;
j = -j;
}
System.out.println(sum);
}
}