如何用Java打出1+1/3+1/5+1/7+...+1/99的答案
2 回答
已采纳
习惯受伤
TA贡献885条经验 获得超1144个赞
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
添加回答
举报
0/150
提交
取消