**5.25(计算PI)使用下面的数列可以近似计算PI:
编写程序,显示当i=10000,20000,.....,100000时PI的值。
**5.25(Compute p) You can approximate p by using the following summation:
Write a program that displays the PI value for i = 10000, 20000, …, and 100000.
下面是参考答案代码:
public class ComputePIQuestion25 {
public static void main(String[] args) {
double piDivideByFour = 0;
for(int i = 1;i <= 100000;i++)
{
piDivideByFour += Math.pow(-1, i+1) / (2 * i - 1);
if(i % 10000 == 0)
System.out.println("The PI value for i = " + i + " is " + piDivideByFour * 4);
}
}
}
运行效果:
注:编写程序要养成良好习惯
1.文件名要用英文,具体一点
2.注释要英文
3.变量命名要具体,不要抽象(如:a,b,c等等),形式要驼峰化
4.整体书写风格要统一(不要这里是驼峰,那里是下划线,这里的逻辑段落空三行,那里相同的逻辑段落空5行等等)
5.普通变量,方法名要小驼峰,类名要大驼峰,常量要使用全部大写加上下划线命名法
6.要学习相应的代码编辑器的一些常用快捷键,如:快速对齐等等