第一章第七题(求π的近似值)(Approximate π)

1.7(求π的近似值)可以使用以下公式计算π:

\LARGE \pi =4\times(1-\frac{1}{3}+\frac{1}{5}-\frac{1}{7}+\frac{1}{9}-\frac{1}{11}+\cdots )

编写程序,显示4\times(1-\frac{1}{3}+\frac{1}{5}-\frac{1}{7}+\frac{1}{9}-\frac{1}{11} ) 和 4\times(1-\frac{1}{3}+\frac{1}{5}-\frac{1}{7}+\frac{1}{9}-\frac{1}{11}+\frac{1}{13})的结果。在程序中用1.0代替1。

1.7 (Approximate π)  can be computed using the following formula:

\LARGE \pi =4\times(1-\frac{1}{3}+\frac{1}{5}-\frac{1}{7}+\frac{1}{9}-\frac{1}{11}+\cdots )

Write a program that displays the result of 4\times(1-\frac{1}{3}+\frac{1}{5}-\frac{1}{7}+\frac{1}{9}-\frac{1}{11} ) and 4\times(1-\frac{1}{3}+\frac{1}{5}-\frac{1}{7}+\frac{1}{9}-\frac{1}{11} + \frac{1}{9}-\frac{1}{13} ).

use 1.0 instead of 1 in your program.

下面是参考答案代码:

public class CalculateTheApproximateNumberOfPIQuestion7 {
	public static void main(String[] args) {
		System.out.println("The Approximate Number Of PI : "
							+ 4 * (1.0 - 1.0 / 3 + 1.0 / 5 - 1.0 / 7
									+ 1.0 / 9 - 1.0 / 11));
		
		System.out.println("The Approximate Number Of PI : "
				+ 4 * (1.0 - 1.0 / 3 + 1.0 / 5 - 1.0 / 7
						+ 1.0 / 9 - 1.0 / 11 + 1.0 / 13));
	}
}

 运行效果:

注:编程程序要养成良好习惯
如:1.文件名要用英文,具体一点
2.注释要英文
3.变量命名要具体,不要抽象(如:a,b,c等等),形式要驼峰化
4.整体书写风格要统一(不要这里是驼峰,那里是下划线,这里的逻辑段落空三行,那里相同的逻辑段落空5行等等)

发布了17 篇原创文章 · 获赞 1 · 访问量 283

猜你喜欢

转载自blog.csdn.net/xjlovewjh/article/details/104105777