Java 水题系列(2)Pi的近似值

2020-09-28 14:56:30 浏览数 (1)

2.Pi的近似值

思路:水题,考察循环和Swing,按题目给的近似式去做就好了,感觉也没啥好说的..

噢对了有意思的是这个级数收敛极慢….运行结果:

右图是输入1000时的结果

代码语言:javascript复制
/** 

 * @Title: b.java

 * @Description: TODO

 * @author 菱形继承

 * @date 2020-03-31 03:19:34

 */



/** 

 * @ClassName: b

 * @Description: TODO

 * @author 菱形继承

 * @date 2020-03-31 03:19:34

*/



import javax.swing.*;

public class b {

    public static void main(String[] args) {

    String user_input=JOptionPane.showInputDialog("please in put an Integer n:");

    int check=Integer.parseInt(user_input);

        double pi=0;

        for(int j = 1;j <= check;j  ){

            pi  =4* Math.pow(-1,j 1) / (2*j-1);

        }

        JOptionPane.showMessageDialog(null, "PI is approximately equal to: " pi);

}

}

0 人点赞