1. Problem description
The Output 99 multiplication table, 1.4 shows.
|
| Fig. 1.4 99 Table of multiplication formulas |
2. Problem analysis
By observing the 99 multiplication tables, you can draw the pattern of the chart: there are 9 rows in total, and the first few lines have several expressions. Also pay attention to the law of each line of expression: line J, the expression starts from j*1, until the end of the j*j, a total of J expressions, the effect can be achieved through a loop. In this case, the output can be controlled by a double loop, the outer loop controls the number of rows, and the inner loop controls the column. There is one more place to note is the relationship between the inner and outer layers, and the number of inner columns is controlled according to the number of rows in the outer layer.
(1) Determine the program framework
From Figure 1.4, we can find that altogether need to print 9 lines, each line and a number of expressions, can be achieved through a double loop, the outer loop control the number of rows, the inner loop control column, so we can write the program framework. The program framework code is as follows:
- public class Ch1_2
- {
- public static void Main (string[] args)
- {
- Outer loop control Number of rows
- for (int i=1;i<10;i++)
- {
- Inner Loop controls the number of expressions per line
- for (int j=1; J<=n; J + +)
- {
- Output expression
- }
- Line ends line break
- System.out.println ();
- }
- }
- }
(2) Finding the number of expressions per line
From Figure 1.4, we can find that the 1th line of an expression, the 2nd line two expressions, the 3rd row of three expressions, ..., the first few lines there are several expressions, so the number of internal loop control column of the variable n equals the number of controls outside the loop, so the inner Loop code can be written as follows:
- for (int j=1; J<=i; J + +)//inner loop controls the number of expressions per line, I represents the number of rows
(3) Expression notation
Expressions are written in the same way: multiplier 1* Multiplier 2 = product. From Figure 1.4, we can find the pattern of each line of expression: line I, the expression starts from i*1, until the end of I*j. Multiplier 1 Unchanged, has been I, in fact, is the number of rows, the multiplier 2 from 1 to J, just as the inner loop variable changes, so the multiplier 2 can be expressed in J. So the expression is written as follows:
- i+ "*" +j+ "=" +i*j//i representative Row, J for column
(4) Complete program
Now we need to combine the procedures we have just made to form our complete program:
- public class Ch1_2
- {
- public static void Main (string[] args)
- {
- Outer loop control Number of rows
- for (int i=1;i<10;i++)
- {
- Inner Loop controls the number of expressions per line
- for (int j=1; J<=i; J + +)
- {
- System.out.print ("+i+" * "+j+" = "+ (i*j)");
- }
- Line ends line break
- System.out.println ();
- }
- }
- }
(5) Operation result
Run the program, as shown in result 1.5.
|
Figure 1.5 Program output results
|
99 Multiplication Table