Results 1 to 13 of 13
  1. #1
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Problem with recursion

    Hi Guys,

    I am trying to simulate a fake Roll Die, where it will always return 1. ( It is a die with two faces, 1 and 0 for simplicity. )

    Here is my code:

    import java.util.Random;

    Java Code:
    public class Dice 
    {
    	int rollDie()
    	{
    		System.out.println("Rolling die..");
    		Random randomGenerator = new Random();
    		int i = randomGenerator.nextInt(2);
    		System.out.println(i + " is the die!");
    		if(i==0)
    		{
    			System.out.println("It is zero so i will roll again.");
    			System.out.println(i);
    			rollDie();
    			i=55;
    		}
    		return i;			
    	}
    And here is a sample output:

    Rolling die..
    0 is the die!
    It is zero so i will roll again.
    0
    Rolling die..
    0 is the die!
    It is zero so i will roll again.
    0
    Rolling die..
    1 is the die!
    55
    Rolling die..
    1 is the die!
    1

    Why am i getting this 55, can anyone help?

    Thank you..

  2. #2
    2by4 is offline Banned
    Join Date
    Dec 2011
    Posts
    143
    Rep Power
    0

    Default Re: Problem with recursion

    Quote Originally Posted by fatabass View Post
    Hi Guys,

    I am trying to simulate a fake Roll Die, where it will always return 1. ( It is a die with two faces, 1 and 0 for simplicity. )

    Here is my code:

    import java.util.Random;

    Java Code:
    public class Dice 
    {
    	int rollDie()
    	{
    		System.out.println("Rolling die..");
    		Random randomGenerator = new Random();
    		int i = randomGenerator.nextInt(2);
    		System.out.println(i + " is the die!");
    		if(i==0)
    		{
    			System.out.println("It is zero so i will roll again.");
    			System.out.println(i);
    			rollDie();
    			i=55;
    		}
    		return i;			
    	}
    And here is a sample output:

    Rolling die..
    0 is the die!
    It is zero so i will roll again.
    0
    Rolling die..
    0 is the die!
    It is zero so i will roll again.
    0
    Rolling die..
    1 is the die!
    55
    Rolling die..
    1 is the die!
    1

    Why am i getting this 55, can anyone help?

    Thank you..
    It is because you are setting i = 55

    Why do you think you shouldn't get 55?

  3. #3
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: Problem with recursion

    Because there is rollDie() before i = 55. And that sets a new value to i, either 0 or 1. If it is zero, again rollDie()...
    i = 55 must be unreachable, at least in my logic. :)
    Obviously it is not.

  4. #4
    2by4 is offline Banned
    Join Date
    Dec 2011
    Posts
    143
    Rep Power
    0

    Default Re: Problem with recursion

    Each method invocation has its own local version of i.

    i is a local variable that sits on the stack. When the function returns, the stack unwinds and it's version of i is gone.

    Do you intend i to be a field instead of local variable?

  5. #5
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: Problem with recursion

    No, but still...

    Should return i; not always get the value from: int i = randomGenerator.nextInt(2); ?

    Because at some point i will be 1 ( after several 0's perhaps, and rolling again due to if().. ) and that is the value I should be getting.

  6. #6
    2by4 is offline Banned
    Join Date
    Dec 2011
    Posts
    143
    Rep Power
    0

    Default Re: Problem with recursion

    your return value is not used anywhere

  7. #7
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: Problem with recursion

    I have a System.out.println(i) in the class where I have my main method and where I call rollDie()..

    Those 55 comes from System.out.println(i)

  8. #8
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: Problem with recursion

    I fixed it.

    Instead of i = 55
    i = roll() makes it work.

    Thanks.

  9. #9
    2by4 is offline Banned
    Join Date
    Dec 2011
    Posts
    143
    Rep Power
    0

    Default Re: Problem with recursion

    the i in the calling method has nothing to do with the i in the called methoc. They just happen to have the same name. You have not used the return value of the method.

    Review method calls and how to store their return values.

    If Norm pops his head round the door, he may have a good reference.

  10. #10
    2by4 is offline Banned
    Join Date
    Dec 2011
    Posts
    143
    Rep Power
    0

    Default Re: Problem with recursion

    Quote Originally Posted by fatabass View Post
    I fixed it.

    Instead of i = 55
    i = roll() makes it work.

    Thanks.
    That's it. Nice one!

    You have set the i in your calling function to the value of the i returned in your called function.

  11. #11
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: Problem with recursion

    Java Code:
    import java.util.Random;
    public class NeverZero 
    {
    	public static void main(String[] args) 
    	{
    		NeverZero app = new NeverZero();
    		System.out.println(app.neverZero());
    	}
    	int neverZero()
    	{
    		Random generator = new Random();
    		int i = generator.nextInt(2);
    		if(i==0)
    		{
    			System.out.println("It is zero...");
    			i = neverZero();
    			return i;
    		}//end if
    		else
    			return i;
    	}//end method neverZero()
    }// end Class
    I wrote a cleaner version.

    Here is a simple output:
    It is zero...
    It is zero...
    It is zero...
    It is zero...
    1


    But it is still confusing to me..

    Why do I need the return in the if block?

  12. #12
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: Problem with recursion

    ... dont mind please.

  13. #13
    2by4 is offline Banned
    Join Date
    Dec 2011
    Posts
    143
    Rep Power
    0

    Default Re: Problem with recursion

    Quote Originally Posted by fatabass View Post
    But it is still confusing to me..

    Why do I need the return in the if block?
    If it got into that block and the return wasn't there, which integer value would it return as promised in the method signature?

Similar Threads

  1. recursion problem
    By katiebear128 in forum New To Java
    Replies: 2
    Last Post: 10-20-2011, 07:19 PM
  2. recursion problem
    By Yakg in forum New To Java
    Replies: 2
    Last Post: 01-05-2011, 02:45 PM
  3. Problem with case - might need recursion
    By Angelar in forum New To Java
    Replies: 6
    Last Post: 10-13-2010, 02:25 PM
  4. Recursion problem
    By luke in forum New To Java
    Replies: 6
    Last Post: 10-06-2010, 06:35 AM
  5. Java Recursion Problem
    By gmnnn in forum Threads and Synchronization
    Replies: 1
    Last Post: 12-06-2009, 04:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •