1: Ordinary implementation of the 99 multiplication table is too simple, is a programmer will be, implemented as follows:
Package test.ms;
public class Test99 {public
static void Main (string[] args) {for
(int i=1; i<=9;i++) {for
(int j=1; j<=i ; J + +) {
System.out.print (j+ "*" +i+ "=" + (I*J) + "");
}
System.out.println ();}}
2: Implement the 99 multiplication table recursively
the code is as follows:
Package test.ms;
public class Multitable {public
static void Main (String args[]) {
m (9);
}
/**
* Print out 99 multiplication table
* @param i * * public
static void m (int i) {
if (i = = 1) {
System.out.println ( "1*1=1");
} else {
m (i-1);
for (int j = 1; J <= I; j +) {
System.out.print (j + "*" + i + "=" + J * i + "");
}
System.out.println ();}}
Recursive way to invoke the diagram:
Each method call will generate a stack frame, pushed into the method stack, when recursive call, the stack frame in the method stack diagram and the diagram above is similar.
It is more intuitive to remove the reference relationship from the stack frame in the method, as shown in the following illustration:
simplifies the corresponding method call final execution as shown in the above illustration, note that I always change j every time starting from 1 and then incrementing to and I equal.
In this way, the above diagram is followed by a stack of 99 multiplication tables:
Summarize:
Nested for loops and comparisons with recursive implementations:
Stack is mainly used to store stack frames, each implementation of a method will appear pressure stack operation, therefore, the use of recursion to generate more stack frames, recursion will affect the memory, very memory consumption, and use for the loop to execute a method, press into the stack frame once, there is only one stack frame, so compare save memory.
Welcome to mercilessly Pat Bricks. Until you hit the halo.