大橙子网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

java近似求pi代码,C语言近似求PI

java运算中,如何引入π进行计算(希望能附上例子)?

Java中Math.PI就表示π。其源码为public static final double PI = 3.14159265358979323846。

成都创新互联公司电话联系:18982081108,为您提供成都网站建设网页设计及定制高端网站建设服务,成都创新互联公司网页制作领域十载,包括成都石雕等多个行业拥有丰富的网站推广经验,选择成都创新互联公司,为企业锦上添花!

比如说你要算半径为2的圆的面积,代码如下:

double pi=Math.PI

int r=2

double S=pi*r*r

运行结果如下图

扩展资料

package 计算π的近似值;

import java.util.Scanner;

public class Example1 {

public static void main(String[] args) {

Scanner scan=new Scanner(System.in);

System.out.println("请输入精度:");

double z=scan.nextDouble();

System.out.println("在精度为"+z+"的条件下,π约等于:\n"+jishuPI(z));

}

static double jishuPI(double z){

double sum=2;

int n=1;

int m=3;

double t=2;

while(tz){

t=t*n/m;

sum=sum+t;

n++;

m+=2;

}

return sum;

}

}

参考资料:Java官网 API-Class Math

Java编程求π问题

public class Test {

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

getPi(10000);

getPi(20000);

}

//输入迭代次数

static void getPi(int n) {

float pi = 0;

for(int i = 1;i=n;i++) {

if(i%2==1) {

pi = pi + (float)1/(2*i-1);

}else {

pi = pi - (float)1/(2*i-1);

}

}

System.out.println("n="+n+";π="+4*pi);

}

}

编写程序:计算π的近似值,π的计算公式为

#includestdio.h

main()

{ int n,i; double t,

sum;/*1*/

printf("请输入n的值\n");

scanf("%d",n);

sum=2; i=1; t=2;/*2*/

while(in) { t=t*(2*i)*(2*i)/(2*i-1)/(2*i+1);

/*3*/ // sum=sum*t; i=i+1; }

printf("π的值=%f\n",t);/*4*/ }

或。

写一个Java程序来实现蒙特卡洛法求π的近似值:

import java.io.BufferedReader;

import java.io.InputStreamReader;

public class MonteCarloPi {

public static void main(String[] args) throws Exception{

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

System.out.print("How many darts shoud I toss at the board?\n");

String s = br.readLine();

int numberOfDarts = Integer.parseInt(s.trim());

double radius = 1.0;

Dartboard d = new Dartboard(radius);

for(int i=1; i=numberOfDarts; i++){

Toss t = Toss.getRandom(radius);

d.strike(t);

}

double fractionIn = d.getFractionIn();

double pi = 4.0 * fractionIn;

System.out.println("Pi is approximately "+pi);

}

}

class Dartboard{

private double radius;

private int insideCircle, outsideCircle;

public Dartboard(double radius){

this.radius = radius;

insideCircle = 0;

outsideCircle = 0;

}

public void strike(Toss toss){

double x = toss.getX();

double y = toss.getY();

if(Math.sqrt(x*x + y*y) radius)

insideCircle++;

else

outsideCircle++;

}

public double getFractionIn() {

double total = (double) (insideCircle + outsideCircle);

return (double) insideCircle/total;

}

}

class Toss{

private double x,y;

public Toss(double x, double y){

this.x = x;

this.y = y;

}

public double getX(){return x;}

public double getY(){return y;}

public static Toss getRandom(double radius){

double x,y;

double size = Math.random();

double sign = Math.random();

size = size * radius;

if(sign 0.5)

x = size;

else

x = -size;

size = Math.random();

sign = Math.random();

size = size * radius;

if(sign 0.5)

y = size;

else

y = -size;

return new Toss(x,y);

}

}

扩展资料:

C语言:用循环结构分别编写程序

#include

void main()

{

\x09int n=1;

\x09float temp;

\x09float sum=0;

\x09do

\x09{

\x09\x09temp=(float)1/(2*n-1);

\x09\x09if(n%2==1)

\x09\x09\x09sum+=temp;

\x09\x09else

\x09\x09\x09sum-=temp;

\x09\x09n++;

\x09}while(temp=0.000001);

\x09printf("Pi=%f\n",sum*4);

}


新闻名称:java近似求pi代码,C语言近似求PI
路径分享:http://dzwzjz.com/article/hddosj.html
在线咨询
服务热线
服务热线:028-86922220
TOP