求解答,一个程序

o_functiongood 2009-12-04 02:22:01
公式:PI=(1-1/3+1/5-1/7+1/9……)*4
应该怎么写 循环2000次吧 用for循环
...全文
156 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
steely_chen 2009-12-04
  • 打赏
  • 举报
回复
double pi=1;
boolean flag=false;
double increase=2;
double currentNum=1;
for(int i=0;i<2000;i++){
currentNum+=increase;
if(flag)
pi+=1/currentNum;
else
pi-=1/currentNum;
flag=!flag;
}
pi=pi*4;
gao512008 2009-12-04
  • 打赏
  • 举报
回复
来晚了 写了这么多了
o_functiongood 2009-12-04
  • 打赏
  • 举报
回复
我刚发完 自己就写出来了
不过也非常谢谢大家了
scarjia 2009-12-04
  • 打赏
  • 举报
回复

public class TestPI {
public static void main(String[] args) {
double sum = 0;
double PI;
for(float i=1; i<=2000; i++) {
sum += ((Math.pow(-1,i+1))*(1/(2*i-1)));
}
PI = sum * 4;
System.out.println(PI);
}
}



这个可以吧,行不?
healer_kx 2009-12-04
  • 打赏
  • 举报
回复
	public static void main(String[] args) {
double pi = 0.0;
int n = 1;
for (int i = 0; i < 2000; i++){
pi = pi + n / (double)(i * 2 + 1.0);
n = -n;
}
pi = pi * 4;
System.out.println(pi);
}
jonay 2009-12-04
  • 打赏
  • 举报
回复

public static void main(String[] args) {
double d = 0.0;
int x = 1;
for (int i = 1; i <= 2000; i++,x*=-1) {
d += 1D/(2*i-1)*x*4;
}
System.out.println(d);
}

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧