使用割圆术求圆周率:
package qiuΠ;
import java.util.Scanner;
public class geyuanshu {
//n为切割次数
static void geyuanshu(int n){
int i,s;
double k,len;
i=0;
k=3.0;
len=1.0;
s=6;
while(i<=n){
System.out.println("第"+i+"次切割,为正"+s+"边形,PI="+k*Math.sqrt(len));
s*=2;
len=2-Math.sqrt(4-len);
i++;
k*=2.0;
}
}
public static void main(String[] args) {
int n;
System.out.println("输入切割次数");
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
geyuanshu(n);
}
}