- /**Recursion Demo 3
- * @author TK Rogers
- * @version 11-06-09
- */
- public class
Mystry {
- public
static void main ( ) {
-
System.out.println ( mystry (3));
- }
-
-
public static int mystry (int y) {
-
System.out.println (" y = " + y);
- if
(y == 0) {
-
System.out.println ("return x = " + 1);
-
-
return 1;
- } else {
-
System.out.println (" else");
-
int x = mystry (y/2);
-
// y = 3 is stored in a stack and the method is re-run
with Y = 1
-
// y = 1 is stored in a stack and the method is re-run with Y = 0
-
// the recursion now stops when 1 is returned. the program
-
// then continues.
-
-
System.out.println (" x = " + x);
-
x *= x;
-
if (y%2 == 1) x *= 3;
-
System.out.println ("return x = " + x);
-
return x;
-
// 1st time through y = 0 the program returns x = 1
-
// 2nd time through y = 1 the program returns x = 3
-
// 2nd time through y = 3 the program returns x = 27
-
// The stack with y values is now empty and the program quits.
- }
- }
- }