-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStackTraceTest.java
38 lines (35 loc) · 989 Bytes
/
StackTraceTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package stackTrace;
import java.util.Scanner;
/**
* A program that displays a trace feature of a recursive method call.
*
* @author Cay Horstmann
* @version 1.01 2004-05-10
*/
public class StackTraceTest {
/**
* Computes the factorial of a number
*
* @param n a non-negative integer
* @return n! = 1 * 2 * . . . * n
*/
public static int factorial(int n) {
System.out.println("factorial(" + n + "):");
Throwable throwable = new Throwable();
StackTraceElement[] frames = throwable.getStackTrace();
for (StackTraceElement f : frames) {
System.out.println(f);
}
int r;
if (n <= 1) r = 1;
else r = n * factorial(n - 1);
System.out.println("return " + r);
return r;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter n:");
int n = in.nextInt();
factorial(n);
}
}