用输出延迟计算Pi

时间:2016-12-03 21:37:07

标签: java loops delay pi

我想计算一定数量的数字,或者我想每10秒钟给一次ouptut。但我没有多少经验,我也不知道如何做到这一点。我试着让它成为可能大约5个小时,但我没有得出结论。 感谢您的时间和帮助!

import java.math.BigInteger;

public class ProjectOne {

    final BigInteger TWO = BigInteger.valueOf(2);
    final BigInteger THREE = BigInteger.valueOf(3);
    final BigInteger FOUR = BigInteger.valueOf(4);
    final BigInteger SEVEN = BigInteger.valueOf(7);

    BigInteger a = BigInteger.ONE;
    BigInteger b = BigInteger.ZERO;
    BigInteger c = BigInteger.ONE;
    BigInteger d = BigInteger.ONE;
    BigInteger e = BigInteger.valueOf(3);
    BigInteger f = BigInteger.valueOf(3);

    public static void main(String[] args) {
        ProjectOne g = new ProjectOne();
        g.Stellenwerte();
    }

    public void Stellenwerte() {
        BigInteger nn, nr;
        boolean first = true;
        while (true) {
            if (FOUR.multiply(a).add(b).subtract(c).compareTo(e.multiply(c)) == -1) {
                System.out.print(e);
                if (first) {
                    System.out.print(".");
                    first = false;
                }
                nr = BigInteger.TEN.multiply(b.subtract(e.multiply(c)));
                e = BigInteger.TEN.multiply(THREE.multiply(a).add(b)).divide(c).subtract(BigInteger.TEN.multiply(e));
                a = a.multiply(BigInteger.TEN);
                b = nr;
                System.out.flush();
            } else {
                nr = TWO.multiply(a).add(b).multiply(f);
                nn = a.multiply((SEVEN.multiply(d))).add(TWO).add(b.multiply(f)).divide(c.multiply(f));
                a = a.multiply(d);
                c = c.multiply(f);
                f = f.add(TWO);
                d = d.add(BigInteger.ONE);
                e = nn;
                b = nr;
            }
        }
    }

    public static void delay(int milliseconds) {
        try {
            Thread.sleep(milliseconds);
        } catch (InterruptedException exp) {
        }
    }
}

0 个答案:

没有答案