Hello,
I know this question might sound stupid!
I am using recursion inside a loop. I want to stop the loop as soon as the method is called recursively and resume it again when the call returns. I am not able to do so. Do I need to lock the method?
Here is my code:
public class Max2Friends {
String[] f = {"NYNNN", "YNYNN", "NYNYN", "NNYNY", "NNNYN"};
int[] twof = {0, 0, 0, 0, 0};
public static void main(String[] args) {
Max2Friends obj=new Max2Friends();
obj.Calculate(999,999);
}
public void Calculate(int p, int t) {
System.out.println("p=" + p);
System.out.println("t=" + t);
for (int i = 0; i != p && i < 5; i++) {
System.out.println("i=" + i);
System.out.println("p value inside=" + p);
System.out.println("t value inside=" + t);
if (i == t)
i=i+1;
for (int j = 0; j < 5; j++) {
if (f[i].charAt(j) == 'Y' && p == 999) {
Calculate(i,j);
} else if (f[i].charAt(j) == 'Y' && j == t) {
twof[p]++;
break;
}
}
}
}
}
The p and t values outside and inside the for loops are different. I want the loops to stop executing as soon as the function is called recursively.I want the recursion to complete and then resume the loops.What seems to be happening is that both are taking place simultaneously due to which I am getting different values for p and t inside and outside the loops.