Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: While loop stopping?

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default While loop stopping?

    I've done the work by hand several times using 25 and I get 5 for an answer but when I run the code it stops after the (second?) loop(by hand its Math.abs(13.5 - 1.9), returning 1.9). Am I missing something or did I screw up altogether? Thanks for your time.

    	static double abs (double b){
    		if (b < 0){
     
    		}
    		return (b*-1);
    	}
     
    	static double sqRoot(double n){
    		if (n<0)return -1;
    		double l = 1.0;
    		double r = n / l;
    		while(Math.abs(l - r) > (0.1)){
    			l = (l + r) / 2.0; 
    			r = n / l;
    		}
    	        return r;       
    	}


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: While loop stopping?

    static double abs (double b){
            if (b < 0){
     
            }
            return (b*-1);
        }
    You're absolute value method is wrong. It will always return -b regardless if it's needed or not. I would strongly recommend using Java's built in absolute value method.

    Math.abs(-1); // 1
    Math.abs(1.234); // 1.234

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    Echo (October 16th, 2010)

  4. #3
    Junior Member
    Join Date
    Oct 2010
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: While loop stopping?

    Oh wow, thank you so much it works now.

Similar Threads

  1. Code stopping, need help fast.
    By aussiemcgr in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 11th, 2010, 09:00 AM
  2. for loop and while loop problems
    By Pulse_Irl in forum Loops & Control Statements
    Replies: 4
    Last Post: May 3rd, 2010, 02:09 AM
  3. Help with my loop
    By Xrrak in forum Loops & Control Statements
    Replies: 2
    Last Post: April 14th, 2010, 08:14 AM
  4. hi. i want to rewrite this do loop into a while loop.
    By etidd in forum Loops & Control Statements
    Replies: 3
    Last Post: January 26th, 2010, 05:27 PM
  5. loop or what
    By silverspoon34 in forum Loops & Control Statements
    Replies: 5
    Last Post: November 19th, 2009, 02:10 PM