1 回答

TA貢獻1797條經(jīng)驗 獲得超4個贊
您對迭代次數(shù)的計算是錯誤的。我已經(jīng)更正了你的代碼。為了方便起見,我還打印了該系列?,F(xiàn)在該值符合預(yù)期。
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);
}
添加回答
舉報