99 Multiplication Table

Source: Internet
Author: User

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:

  1. public class Ch1_2
  2. {
  3. public static void Main (string[] args)
  4. {
  5. Outer loop control Number of rows
  6. for (int i=1;i<10;i++)
  7. {
  8. Inner Loop controls the number of expressions per line
  9. for (int j=1; J<=n; J + +)
  10. {
  11. Output expression
  12. }
  13. Line ends line break
  14. System.out.println ();
  15. }
  16. }
  17. }

(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:

    1. 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:

    1. 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:

  1. public class Ch1_2
  2. {
  3. public static void Main (string[] args)
  4. {
  5. Outer loop control Number of rows
  6. for (int i=1;i<10;i++)
  7. {
  8. Inner Loop controls the number of expressions per line
  9. for (int j=1; J<=i; J + +)
  10. {
  11. System.out.print ("+i+" * "+j+" = "+ (i*j)");
  12. }
  13. Line ends line break
  14. System.out.println ();
  15. }
  16. }
  17. }

(5) Operation result

Run the program, as shown in result 1.5.

Figure 1.5 Program output results

99 Multiplication Table

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.