用java求圆周率π的近似值
π=4-4/3+4/5-4/7+4/9-4/11……回答程序要计算多少项才能得到数值3.14,3.141,3.1415,3.14159...
π=4-4/3+4/5-4/7+4/9-4/11……
回答程序要计算多少项才能得到数值3.14,3.141,3.1415,3.14159 展开
回答程序要计算多少项才能得到数值3.14,3.141,3.1415,3.14159 展开
2个回答
展开全部
package xiangqi2;
public class Test3 {
public static void main(String[] args) {
int time = 0;
float sum = 0;
float num = 1;
int mark1=0;
int mark2=0;
int mark3=0;
int mark4=0;
while (true) {
time++;
sum = sum + 4 / num;
//System.out.println(sum);
String s = "" + sum;
if (mark1==0&&s.length() >= 4 && s.substring(0, 4).equals("3.14")) {
mark1++;
System.out.println(time);
}
if (mark2==0&&s.length() >= 5 && s.substring(0, 5).equals("3.141")) {
mark2++;
System.out.println(time);
}
if (mark3==0&&s.length() >= 6 && s.substring(0, 6).equals("3.1415")) {
mark3++;
System.out.println(time);
}
if (mark4==0&&s.length() >= 7 && s.substring(0, 7).equals("3.14159")) {
mark4++;
System.out.println(time);
break;
}
num = -num / Math.abs(num) * (Math.abs(num) + 2);
}
}
}
==================
结果:
119
1696
10188
159784
public class Test3 {
public static void main(String[] args) {
int time = 0;
float sum = 0;
float num = 1;
int mark1=0;
int mark2=0;
int mark3=0;
int mark4=0;
while (true) {
time++;
sum = sum + 4 / num;
//System.out.println(sum);
String s = "" + sum;
if (mark1==0&&s.length() >= 4 && s.substring(0, 4).equals("3.14")) {
mark1++;
System.out.println(time);
}
if (mark2==0&&s.length() >= 5 && s.substring(0, 5).equals("3.141")) {
mark2++;
System.out.println(time);
}
if (mark3==0&&s.length() >= 6 && s.substring(0, 6).equals("3.1415")) {
mark3++;
System.out.println(time);
}
if (mark4==0&&s.length() >= 7 && s.substring(0, 7).equals("3.14159")) {
mark4++;
System.out.println(time);
break;
}
num = -num / Math.abs(num) * (Math.abs(num) + 2);
}
}
}
==================
结果:
119
1696
10188
159784
展开全部
public class Test3 {
public static void main(String[] args) {
int time = 0;
float sum = 0;
float num = 1;
while (true) {
time++;
sum = sum + 4 / num;
//System.out.println(sum);
String s = "" + sum;
if (s.length() >= 7 && s.substring(0, 7).equals("3.14159")) {
System.out.println(time);
break;
}
num = -num / Math.abs(num) * (Math.abs(num) + 2);
}
}
}
=================
结果是:
159784
public static void main(String[] args) {
int time = 0;
float sum = 0;
float num = 1;
while (true) {
time++;
sum = sum + 4 / num;
//System.out.println(sum);
String s = "" + sum;
if (s.length() >= 7 && s.substring(0, 7).equals("3.14159")) {
System.out.println(time);
break;
}
num = -num / Math.abs(num) * (Math.abs(num) + 2);
}
}
}
=================
结果是:
159784
参考资料: http://zhidao.baidu.com/question/70667154.html
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |