Results 1 to 13 of 13
Thread: Problem with recursion
- 12-15-2011, 10:02 PM #1
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
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;
And here is a sample output: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; }
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..
- 12-15-2011, 10:16 PM #2
Banned
- Join Date
- Dec 2011
- Posts
- 143
- Rep Power
- 0
- 12-15-2011, 10:18 PM #3
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
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.
- 12-15-2011, 10:23 PM #4
Banned
- Join Date
- Dec 2011
- Posts
- 143
- Rep Power
- 0
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?
- 12-15-2011, 10:25 PM #5
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
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.
- 12-15-2011, 10:27 PM #6
Banned
- Join Date
- Dec 2011
- Posts
- 143
- Rep Power
- 0
Re: Problem with recursion
your return value is not used anywhere
- 12-15-2011, 10:29 PM #7
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
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)
- 12-15-2011, 10:31 PM #8
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
Re: Problem with recursion
I fixed it.
Instead of i = 55
i = roll() makes it work.
Thanks.
- 12-15-2011, 10:34 PM #9
Banned
- Join Date
- Dec 2011
- Posts
- 143
- Rep Power
- 0
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.
- 12-15-2011, 10:36 PM #10
Banned
- Join Date
- Dec 2011
- Posts
- 143
- Rep Power
- 0
- 12-15-2011, 10:40 PM #11
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
Re: Problem with recursion
I wrote a cleaner version.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
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-15-2011, 10:45 PM #12
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
Re: Problem with recursion
... dont mind please.
- 12-15-2011, 10:46 PM #13
Banned
- Join Date
- Dec 2011
- Posts
- 143
- Rep Power
- 0
Similar Threads
-
recursion problem
By katiebear128 in forum New To JavaReplies: 2Last Post: 10-20-2011, 07:19 PM -
recursion problem
By Yakg in forum New To JavaReplies: 2Last Post: 01-05-2011, 02:45 PM -
Problem with case - might need recursion
By Angelar in forum New To JavaReplies: 6Last Post: 10-13-2010, 02:25 PM -
Recursion problem
By luke in forum New To JavaReplies: 6Last Post: 10-06-2010, 06:35 AM -
Java Recursion Problem
By gmnnn in forum Threads and SynchronizationReplies: 1Last Post: 12-06-2009, 04:22 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks