1. Senior Member
Join Date
Nov 2011
Location
Turkey
Posts
380
Blog Entries
24
Rep Power
6

## 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. Banned
Join Date
Dec 2011
Posts
143
Rep Power
0

## Re: Problem with recursion

Originally Posted by fatabass
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. Senior Member
Join Date
Nov 2011
Location
Turkey
Posts
380
Blog Entries
24
Rep Power
6

## 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. 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?

5. Senior Member
Join Date
Nov 2011
Location
Turkey
Posts
380
Blog Entries
24
Rep Power
6

## 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. Banned
Join Date
Dec 2011
Posts
143
Rep Power
0

## Re: Problem with recursion

your return value is not used anywhere

7. Senior Member
Join Date
Nov 2011
Location
Turkey
Posts
380
Blog Entries
24
Rep Power
6

## 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. Senior Member
Join Date
Nov 2011
Location
Turkey
Posts
380
Blog Entries
24
Rep Power
6

## Re: Problem with recursion

I fixed it.

i = roll() makes it work.

Thanks.

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.

10. Banned
Join Date
Dec 2011
Posts
143
Rep Power
0

## Re: Problem with recursion

Originally Posted by fatabass
I fixed it.

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. Senior Member
Join Date
Nov 2011
Location
Turkey
Posts
380
Blog Entries
24
Rep Power
6

## 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. Senior Member
Join Date
Nov 2011
Location
Turkey
Posts
380
Blog Entries
24
Rep Power
6

13. Banned
Join Date
Dec 2011
Posts
143
Rep Power
0

## Re: Problem with recursion

Originally Posted by fatabass
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?

#### Posting Permissions

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