1 回答
TA贡献1797条经验 获得超4个赞
您对迭代次数的计算是错误的。我已经更正了你的代码。为了方便起见,我还打印了该系列。现在该值符合预期。
public static void main(String[] args) {
Scanner kb=new Scanner(System.in);
System.out.println("Enter Limit");
double limit=kb.nextDouble();
int TermsSum=1;
double min=1;
double PiVal= 4.0 / min;
double PiCon = 4.0 / min;
System.out.print("4.0 /" + min);
while (limit<=PiCon) {
if (limit <= PiCon) {
TermsSum++;
min += 2;
PiCon = (4.0 / min);
PiVal += (-(PiCon));
System.out.print("-"+"4.0 /" + min);
}
if (limit <= PiCon) {
TermsSum++;
min += 2;
PiCon = (4.0 / min);
PiVal += (PiCon);
System.out.print("+"+"4.0 /" + min);
}
}
System.out.println("Limit: "+limit);
System.out.println("Pi Value: "+PiVal);
System.out.println("Terms Summed: "+TermsSum);
}
添加回答
举报