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: my first recursion ,what did i do wrong?

  1. #1
    Member
    Join Date
    Nov 2013
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default my first recursion ,what did i do wrong?

    public static boolean q8(int n){
    	boolean check=true;
     
    		if((n/10)<0){
    			return check;
    		}
    		int f=n%10;
    		int r=(n/10)%10;
    		if((f%2==0)&&(r%2==0)){
    			return check=false;
    		}
    		q8(n/10);
    		return check;
     
     
    	}

    the recursion need to get a number and return true if each pair of digits are one even and the other odd.
    false if not.
    how can i write it better ?for example:for the number 230 -->return true(which is correct)
    but for the number 243---->return true(which should be false) cause it takes 2 and 0 from the begining.
    how can i rewrite correct?

    --- Update ---

    public static boolean q8(int n){
     
    		int f=n%10;
    		int r=(n/10)%10;
    		if((f%2==0)&&(r%2==0)&&((n/10)>0)){
    			return false;
    		}
     
     
    		return q8(n/10);
     
     
     
    	}
    this is my second try.it works only if the numbers is false.on true numbers it doesnt work?
    any idea how to correct me?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: my first recursion ,what did i do wrong?

    Have you tried debugging the code to see what it is doing so you can determine what needs to be changed to make it work the way you want? I use println() statements that print out the values of all the variables as they are given values and also the value of args passed to a method.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: my first recursion ,what did i do wrong?

    the recursion need to get a number and return true if each pair of digits are one even and the other odd.
    false if not.
    Can you explain this better?

    Show what you mean by "each pair of digits." For example, using your 230, I would think the pairs of digits would be 23, 30, and maybe 20. For 243, 24, 43, and maybe 23. Use an example and show which pairs of digits the program should consider. Then, considering the pairs of digits, show what the method should return, true or false, and explain why.

Similar Threads

  1. Replies: 4
    Last Post: March 6th, 2014, 05:14 PM
  2. RECURSION
    By kevthanewversi in forum Algorithms & Recursion
    Replies: 5
    Last Post: April 21st, 2013, 06:56 AM
  3. Recursion Help
    By WeaKeN in forum Algorithms & Recursion
    Replies: 10
    Last Post: May 27th, 2011, 08:17 AM
  4. [SOLVED] Recursion help
    By Actinistia in forum Java Theory & Questions
    Replies: 3
    Last Post: March 21st, 2011, 12:26 PM
  5. Recursion Help
    By vmr in forum Algorithms & Recursion
    Replies: 3
    Last Post: April 1st, 2010, 11:27 PM