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 4 of 4

Thread: A variable that never change.

  1. #1
    Junior Member
    Join Date
    Aug 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question A variable that never change.

    Hi.

    I have an issue with my code:
    public class BuildBridge {
     
    	public static void main(String[] args) {
     
    		BuildBridge cartas = new BuildBridge();
     
    		int res = cartas.howManyCards(1,1);
     
    		System.out.print(res);
     
     
    	}
     
    	public int howManyCards(int d, int l) {
     
    		int results = 0;
    		double middle = l/2;
    		double increment = 0.0;
     
    		while (middle + increment < d) {
     
     
    			++results;
    			increment += middle / results; //
     
     
    		} 
     
    		return results;
    	}
     
    }

    The "increment" and "middle" variables are always zero, never change, even "middle" after the asignation "= l / 2". Only the variable "results" change when the while loop comes.

    Hope you can help, me.

    Thanks, have a nice day.


    Eclipse Standard/SDK

    Version: Kepler Release
    Build id: 20130614-0229
    Last edited by helloworld922; August 14th, 2013 at 01:01 PM. Reason: please use [code] tags


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: A variable that never change.

    When posting code, please use the highlight tags to preserve formatting. You can edit your post and add them now.

    Have you stepped through this with a debugger, or at least added print statements, to figure out what's going on? When exactly does the code execution differ from what you expect?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    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: A variable that never change.

    You're relying on integer math to calculate middle. You need to be using floating point math.

    The easiest way is to add a decimal to the 2.

  4. #4
    Junior Member
    Join Date
    Aug 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by helloworld922 View Post
    You're relying on integer math to calculate middle. You need to be using floating point math.

    The easiest way is to add a decimal to the 2.
    Thank you !!! that was the problem.

Similar Threads

  1. Instance Variable Won't Change!!
    By wawawarrior in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 5th, 2013, 11:07 PM
  2. Permanently change variable values based on user input.
    By joseph.clevenger7 in forum Java Theory & Questions
    Replies: 3
    Last Post: April 26th, 2013, 07:32 PM
  3. Replies: 18
    Last Post: March 30th, 2013, 09:11 AM
  4. [SOLVED] While loop won't allow variable to change
    By Thor in forum Loops & Control Statements
    Replies: 13
    Last Post: October 9th, 2012, 08:35 PM
  5. write the program in C, C++, Java or C#. Change variable types accordingly.
    By indrasensingh01 in forum Java Theory & Questions
    Replies: 2
    Last Post: October 8th, 2012, 06:34 PM