如何用Java打出1+1/3+1/5+1/7+...+1/99的答案
如何用Java打出1+1/3+1/5+1/7+...+1/99的答案
2回答
-
习惯受伤
public class Main {
public static void main(String[] args) throws Exception {
double total = 0;
for (int i = 1; i <= 99; i+=2) {
total += 1f / i;//1可以看成是1/1
}
System.out.println("total = "+total);
}
}结果:total = 2.9377748798578978
-
慕粉4075985
int i = 1;
float sum = 0;
while(i<=99){
sum = sum+(1f/i);
i=i+2;
}