Yep - integer division truncates. 
--- Tim Nicholson <[EMAIL PROTECTED]> wrote:
> 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.
> 
> 
> 
> 
> ----- Original Message -----
> From: "Stefan Carlsson" <[EMAIL PROTECTED]>
> To: "JDJList" <[EMAIL PROTECTED]>
> Sent: Monday, September 23, 2002 8:39 PM
> Subject: [jdjlist] SV: having a problem that I can't understand....
> 
> 
> 
> Well... haven't give your calculations any thought
> (guess you know what you are doing...), but first
> you must increse your variable "i" (like i++) somewhere
> within your while -loop, or your will never get out
> of that scoop. Also, perhaps you must "flush();" your
> ScreenWriter (?). Don't know if it works like the
> "OutputSreamWriter" -class ?, which must bee flushed
> to print anything (default). Is "ScreenWriter" part
> of jdk 1.4 ?
> 
> Regards
> stefan
> 
> 
> 
> -----Ursprungligt meddelande-----
> Fr�n: Tim Nicholson [mailto:[EMAIL PROTECTED]]
> Skickat: den 22 september 2002 15:52
> Till: JDJList
> �mne: [jdjlist] having a problem that I can't understand....
> 
> 
> Hi,
> 
> can somebody please help me ?
> 
> I am trying to run the following code :-
> 
> 
> ----------------------------------------------------------------------------
> -------------
> import TerminalIO.*;
> 
> public class Liebniz {
> 
>    KeyboardReader reader = new KeyboardReader();
>    ScreenWriter writer = new ScreenWriter();
> 
>    int i;
>    int x;
>    int numiter;
>    double valuesofar;
> 
> //================================================================
>    public void run() {
> 
> 
>    numiter = reader.readInt("Enter number iterations for Pi value: ");
> 
> 
>    i = 1;
>    x = 1;
>    valuesofar = 0;
> 
>    while (i <= numiter) {
> 
>     if(i % 2 == 0){
>     valuesofar = valuesofar  - (1 / x);
>     }
>     else {
>      valuesofar = valuesofar + (1 / x);
>    }
> 
>    x = x + 2;
> 
>   }
> 
>    writer.println("The value so far is: " + valuesofar);
> 
> 
>    }
> 
> //=================================================================
>    public static void main(String [] args) {
>       Liebniz tpo = new Liebniz();
>       tpo.run();
>    }
> }
> 
> ----------------------------------------------------------------------------
> ---------------------
> 
> 
> And when I execute it, it succeeds in asking me to enter a number of
> iterations but then after I have entered in an integer and push enter,
> nothing more happens. In other words it only gets as far as
> the cursor going onto the next line. But nothing more happens.
> 
> So it doesnt display any output to my input.
> The cursor just goes to the next line and stops and I have to push the X
> button in the top right hand corner of the window to get it to close. What
> usually happens is that it runs its output and then says "push any key to
> continue " but when I try this program it doesnt do any of that. It just
> sits on the next line after I have entered my input and it doesnt display
> anything more.
> 
> Can someone please help me with this ?
> 
> I get no compile errors by the way with this.
> 
> The program is also attached as a .java file if that makes things any easier
> ?
> 
> 
> 
> To change your JDJList options, please visit:
> http://www.sys-con.com/java/list.cfm
> 
> 
> 
> 
> To change your JDJList options, please visit: http://www.sys-con.com/java/list.cfm


=====

Mark Zawadzki Performance Engineer/DBA/Programmer extraordinaire� [EMAIL PROTECTED] 
[EMAIL PROTECTED]

 "Democracies die behind closed doors," - Judge Damon Keith


__________________________________________________
Do you Yahoo!?
New DSL Internet Access from SBC & Yahoo!
http://sbc.yahoo.com

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

Reply via email to