编写java程序,计算π的近似值,用π=4(1-1/3+1/5-1/7+……+(-1)^i+1/2i-1)
时间: 2023-05-31 08:01:58 浏览: 116
程序设计-π的近似值.pptx
public class CalculatePi {
public static void main(String[] args) {
double pi = 0;
int i = 0;
while (true) {
double term = Math.pow(-1, i) / (2 * i + 1);
if (term == 0) {
break;
}
pi += term;
i++;
}
pi *= 4;
System.out.println("π ≈ " + pi);
}
}
阅读全文