This is the 17th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

Like it and see. Make it a habit. Wechat search [a coding] follow this programmer who is struggling in the Internet.

This article is included in the technical expert training, which contains my learning route, series of articles, interview question bank, self-study materials, e-books, etc. Welcome to star ⭐ ️

Topic describes

Difficulty: Easy

Print the multiplication table sequentially

knowledge

  • Double for loop
  • String splicing

Their thinking

If you look closely at the multiplication table, it is not difficult to find that it is actually half of a square matrix. In combination with the exercise in the previous section on two-dimensional arrays, consider using a double for loop.

The difficulty is how to determine the boundary values of the loop.

Observe that the first row has one column, the second row has two columns, and the third row has three columns…

Then row I has column I. If the outer loop is regarded as row printing and the inner loop as column printing, then:

for (int i = 1; i < 10; i++) {
	for (int j = 1; j <=i; J++) {... }}Copy the code

Finally, I,j, I *j can be spliced with a + sign.

Don’t forget to add a space at the end and a line break at the end of each line.

Code implementation

/** * Prints the multiplication table * a coding */
public class question_12 {
    public static void main(String[] args) {
        / / print
        for (int i = 1; i < 10; i++) {
            / / print columns
            for (int j = 1; j <= i; j++) {
                // Print without newline system.out. print without ln
                System.out.print(j+ "x" + i + "=" + i * j + "");
            }
            / / a newlineSystem.out.println(); }}}Copy the code

The output

Expand the summary

How to write a reverse print, like this:

Just change the outer loop to for (int I = 9; i >=1; I -).

The last

One foot is difficult to go, one hand is difficult to sing, a person’s power is limited after all, a person’s journey is doomed to be lonely. When you set a good plan, with full of blood ready to set out, must find a partner, and Tang’s monk to the West, the master and his disciples four people united as one to pass the ninety-eight difficult. So,

If you want to learn Java well

Want to go to company

Want to get high salary

Want to have a group of like-minded partners

Please join the technical exchange