英语翻译这是一个java编程题,题目是英文的,想要翻译,如果能完成程序编写,追加100分Pi can be calcul
英语翻译这是一个java编程题,题目是英文的,想要翻译,如果能完成程序编写,追加100分Pi can be calculated using the following sum,which is an approximation to Pi/4 :PiSum = 1 - 1/3 + 1/5 - 1/7 + ...+ (-1)^n / (2*n + 1)1) Write two methods that return PiSum for a specified value of n,one methodusing recursion and the other using iteration to do the sum.Hint:instead of calculating (-1)^n,you can check whether the nth term isnegative by checking whether n%2 (i.e.n modulo 2) is 1.Assume that the methods are always called with a non-negative value of n,so youdon't need to worry about dealing with invalid parameter values.2) Show the additional code you would add to the above methods to throw anexception if n is less than zero (you don't need to rewrite the whole method).Theexception handler should generate an error message saying that the number of termsin the sum must not be negative.Write a code fragment (i.e.it doesn't need to be a complete method) that invokesone of the methods for calculating Pi and prints out the result if the method returnscorrectly,or else catches an exception and prints out the error message returned.3) When calculating Pi using the given formula,suppose that instead of passing aparameter specifying the number of terms n in the sum,we instead want to pass aparameter that specifies a precision value.The sum should terminate at the first valuen for which the absolute value of the nth term in the sum,i.e.1/(2*n+1) is less thanthe specified precision.Show how this can be implemented using a do loop.
最佳回答
我有一个类似的代码。将就着用把大概意思是一样的只是没有异常处理public class a2and5{public static void main(String [] agrs){int count=0;double pi=0。0,l=1。0,diff,pi1;diff=1;while(Math。abs(diff)>0。01){ pi1=pi;pi+=((4。0/(count*2。0+1。0))*l); l*=-1。0;System。out。print(pi+" "+count+"\n");count++;diff=pi1-pi;}}}
最新回答共有2条回答
-
2026-04-07 16:43:23受伤的盼望
回复我有一个类似的代码。将就着用把大概意思是一样的只是没有异常处理public class a2and5{public static void main(String [] agrs){int count=0;double pi=0。0,l=1。0,diff,pi1;diff=1;while(Math。abs(diff)>0。01){ pi1=pi;pi+=((4。0/(count*2。0+1。0))*l); l*=-1。0;System。out。print(pi+" "+count+"\n");count++;diff=pi1-pi;}}}
热门文章
- 康达学院专转本五年制
- 高考一个考场分ab卷吗
- not only but also用法
- 某物体做自由落体运动,从释放开始计时,则物体在前2s内的平均速度为______m/s,物体下落2m时的速度大小为______m/s.
- 三角函数公式大全表格
- 地理中考必背知识点2022
- 2013-2014学年小学六年级科学上学期期末考试试卷及答案
- 人教版2014-2015学年小学五年级英语第二学期期中教学质量检测试卷及答案
- 【Linux驱动开发】设备树详解(二)设备树语法详解
- 别跟客户扯细节
- 在别的城市买房子能落户吗
- 卖房前要把装修贷还完吗
- 高中政治教学提高教学效果的方法探究
- “互联网+”背景下的初中英语课堂教学改革与创新策略研究
- 2022年终止合同范本
- 租房合同范本范文
- 如何挑选土豆
- 如何挑选土鸡
