比较/评估Java中的数字

时间:2014-10-26 11:41:22

标签: java while-loop numbers compare pi

我试图找到一种方法将IntPi的当前值与我的代码中的IntPi的最后一个值进行比较有点困难,我们本周的任务问题是:

第1部分:( 80%的分数): 使用WHILE或DO-WHILE LOOP使用以下等式编写程序来计算PI:PI = 3 + 4 /(2 * 3 * 4) - 4 /(4 * 5 * 6)+ 4 /(6 * 7 * 8) - 4 /(8 * 9 * 10)+ ...允许用户指定要在计算中使用的术语数(显示5个术语)。每次循环时,只应将一个额外的术语添加到PI的估计值中。

第2部分:(占20%的分数): 改变第一部分的解决方案,以便允许用户指定1到8位数之间所需的精度(即正确的位数;例如,5位数PI是3.14159),而不是术语数。应该改变循环上的条件,使其继续直到获得所需的精度。请注意,您只需提交该程序的第二个版本(假设您已正常工作)。

我已经完成了第一部分,代码在下面,虽然我想找到一种方法让程序在IntPi的当前值等于最后一个值(应该发生,当IntPi大约314159265)。我不知道该怎么做。我知道while语句应该是' while(currentValue!= lastValue)',虽然我不知道如何比较while循环中的值。

非常感谢所有帮助!

我已将某些内容设置为语句(即扫描程序和输出),因为它们不是查找答案所必需的。

import java.util.Scanner;

public class ComputePI {

public static void main(String[] args) {

    /*
    System.out.print( "Please enter the amount of  "
            + "digits of PI, you would like to set it too");
    Scanner termScan = new Scanner( System.in );
    int term = termScan.nextInt();
    termScan.close(); 
    */
    double pi = 3.0;
    int loopCount = 2;
    int number = 2;
    int IntPi = 0;

        while ( )
        {               
            if (loopCount % 2 == 0)
            {
                pi = pi + ( 4.0/ ((number)*(number+1)*(number+2)) );
            }
            else if ( loopCount % 2 != 0 )
            {
                pi = pi - ( 4.0 / ((number)*(number+1)*(number+2)));
            }


            IntPi = (int) (pi * 100000000);
            number = number + 2;

            loopCount++;

        } 

        //System.out.print( "The Value of Pi in " +
                //" terms is equal to " + IntPi);
     }

}

2 个答案:

答案 0 :(得分:1)

在修改之前,只需将pi的旧值存储在其他变量中:

    double pi = 3.0;
    double oldPi = 0.0;
    ...
    while (Math.abs(oldPi-pi)>0.000001)           
    {        
        oldPi = pi;       
        if (loopCount % 2 == 0)
        {
            pi = pi + ( 4.0/ ((number)*(number+1)*(number+2)) );
        }
        else if ( loopCount % 2 != 0 )
        {
            pi = pi - ( 4.0 / ((number)*(number+1)*(number+2)));
        }
        ...
    }

编辑:

我没有仔细阅读你的问题,这导致比较错误的变量(即使结果仍然不错,并且比较int变量的原理相同)。

以下是可以比较IntPi

中的更改的更改
      double pi = 3.0;
      int loopCount = 2;
      int number = 2;
      int IntPi = 0;
      int prevIntPi = -1;

      while (IntPi != prevIntPi)
      {               
        if (loopCount % 2 == 0)
        {
          pi = pi + ( 4.0/ ((number)*(number+1)*(number+2)) );
        }
        else if ( loopCount % 2 != 0 )
        {
          pi = pi - ( 4.0 / ((number)*(number+1)*(number+2)));
        }

        prevIntPi = IntPi;
        IntPi = (int) (pi * 100000000);
        number = number + 2;

        loopCount++;
      } 

答案 1 :(得分:0)

double pi     = 3.0; 
int loopCount = 2; 
int number    = 2; 
int oldPi     = 0; 
int i         = 0; 
int term      = 8; 

// an integer we use to transfropm double pi 
// to integer pi with the needed precision 

int tt        = (int)Math.pow(10.0,term); 

// compare old PI and new PI in integer space
// based on tt e.g 1000 if term==3

while ( (int)(pi*tt) != oldPi)    {                
    oldPi  = (int) (pi * tt); 
    if (loopCount % 2 == 0)         { 
        pi = pi + ( 4.0/ ((number)*(number+1)*(number+2)) ); 
    } else if ( loopCount % 2 != 0 )    { 
        pi = pi - ( 4.0 / ((number)*(number+1)*(number+2))); 
    } 

    number = number + 2; 

    loopCount++; 
}