Vote count: 0
This is a rather quick question. I have been having trouble understanding the concept of recursion in Java and I was wondering if someone could help me. I have been tracing successfully 5 out of the 6 recursive methods that I have done, but this one is throwing me for a loop, literally. I was expecting the output to be just 2, but when I put it into my compiler it came out with 1213121. As I said, I have been able to do this thus far, but this one is confusing me. Here is what I am working with:
public class Recursion
{
public static void main(String [] args)
{
Recursion r = new Recursion();
r.doSomething(3);
}
public void doSomething(int n)
{
if (n > 0)
{
doSomething(n-1);
System.out.print(n);
doSomething(n-1);
}
}
}
asked 59 secs ago
Having trouble tracing recursive method in Java
Aucun commentaire:
Enregistrer un commentaire