Ok... So then I guess you will find a solution to your problem...
Made a little test myself, if you can't make it work... But if you
cheat and look at my solution you won't learn much... Try your own
solution first before looking at my code (at the bottom of this page...)

You have to do quite a lot of iterations to get a fair calculation
(over 1,000,000 or so...)


Regards
stefan





-----Ursprungligt meddelande-----
Fr�n: Tim Nicholson [mailto:[EMAIL PROTECTED]]
Skickat: den 23 september 2002 13:49
Till: JDJList
�mne: [jdjlist] Re: SV: having a problem that I can't understand....


Stefan,

thanks for that help. I actually managed to figure out that I had an
infinite loop after a while and I did it by sticking in a println("what is
happening here ?") statement just inside the opening bcurly bracket of thw
while loop and then when I did that and then executed the program, an
inifinte number of "what is happening here ?" statements in my output. So
that gave me the clue that I didn't increment or adjust the variable that
affected my condition of my while loop.

So I eventually figured that part out. The part that I didnt figure out was
the problem that I had afterwards when my output gave me a value that wasn't
quite right.

But somoene suggested to me that that may be due to using integer arithmetic
rather than double or float arithmetic. So tomorrow I am going to try that
and see if that improves things.

ScreenWriter and KeyboardReader are part of the

TerminalIO.* package that comes along with the Lambert and Osborne book --
Java a Framework for Programming and Problem Solving.












































































import java.io.LineNumberReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class Liebniz extends Thread {
    private InputStreamReader input = null;
    private LineNumberReader reader = null;

    public Liebniz() {
        try {
            input = new InputStreamReader(System.in);
            reader = new LineNumberReader(input);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void run() {
        // pi/4 = 1 - 1/3 + 1/5 - 1/7 + ...
        System.out.print("Enter number iterations for Pi value: ");
        try {
            int numiter = Integer.parseInt(reader.readLine());
            double valuesofar = 0.0;
            int sign = 1;

            for (int i = 0; i <= numiter; i++) {
                valuesofar += sign*(1 / (double)(2*i+1));
                sign = -sign;
            }
            System.out.println("valuesofar: " + valuesofar);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String [] args) {
        Liebniz tpo = new Liebniz();
        tpo.run();
    }
}

To change your JDJList options, please visit: http://www.sys-con.com/java/list.cfm

Reply via email to