java用do-while计算累加

public class Yang1014 {
public static void main(String[] args) {
//要求使用while,计算1+1/3+1/5+1/7…+1/(2×n+1),计算到1/(2×n+1)小于0.00001时为止,显示上述表达式中的n值和计算结果
double n=1,sum=0,temp;

do {
	temp=1/(2*n+1);
	sum+=temp;
	n++;
}while(temp>=0.00001);
System.out.printf("n=%.0f\n",n-1);
System.out.println("sum="+sum);

}
}

猜你喜欢

转载自blog.csdn.net/weixin_44864260/article/details/102816998