Use the for Loop in Java and the if statement to print a detailed analysis of the hollow Diamond (you can repeat it to deepen your understanding !), Javaif

Source: Internet
Author: User

Use the for Loop in Java and the if statement to print a detailed analysis of the hollow Diamond (you can repeat it to deepen your understanding !), Javaif

Let's just stop talking about it. Let's get the code first:

(Figure 1)

As shown in the above Code, the execution sequence of the program is from left to right, from top to bottom, so when we want to print a hollow Diamond 2 in the DOS window

(Figure 2)

Time: We can divide it into the upper and lower parts,

In Figure 1, we first create a struct object through its nextInt () method receive an int type value input by the keyboard from the DOS window (for details about the category, see JavaAPI ). Then assign this number to the total variable, that is, the total number of hollow diamond rows that we want to print.

Using total, we can specify the number of rows in the upper half, half, and the number of rows in the lower half, halfMore, respectively.

In this case, we will perform the following two steps: (total = 9 is used as an example)

Upper part:

At this time, the number of rows in the upper part is three rows. First, an outer statement is used to control the number of rows to be printed.

The internal content of this loop:

Each cycle is from left to right. In the print order from top to bottom, what should each row print in sequence!

The printing process involves three main elements: number of rows, number of spaces, and number of stars;

A secondary element: line feed.

First, because we allow the outer loop to control the "number of rows", we need to analyze the relationship between the number of spaces and the number of rows, the number of stars, and the number of rows respectively:

Number of spaces on the left and number of rows:

Number of spaces on the left of line n:

1 4

2 3

3 2

4 1

It can be seen that when the number of rows is increasing by an equal deviation, the number of spaces on the left is decreasing by an equal difference.

That is, a1 = 4;

A2 = 3;

A3 = 2;

A4 = 1; the general formula of the arithmetic difference series: an = a1 + (n-1) * d: an = 3 + (-1) (n-1) = 3-n + 1.

That is, the number of spaces on the Left = number of rows-number of rows + 1.

Print the second line of code in a for loop.

After the space on the left is printed, an asterisk is printed, and the number of spaces in the middle is printed:

Number of intermediate spaces and number of rows:

Number of spaces in the middle of line n

1 0

2 1

3 3

4 5

Similar to the left-side space query, the difference is that all items except the first item increase proportionally.

Because we use a for loop and the cycle initialization condition is 1, when our cycle condition is negative, the number of printing times is also 0.

So we can set the first item to-1, and there are: an =-1 + (n-1) * 2 = 2 * n-3. This includes the code of Line 3 in Figure 1.

(4) The row judgment statement is used to print the asterisks on the right except the first line. At last, the loop of the number of rows in the outermost layer changes the row every time it is switched.

Lower half: The analysis logic is the same as the above half, that is, the opposite of the half.

Finally, remember that during programming:

The idea must be clear at all times!

The idea must be clear at all times!

The idea must be clear at all times!

 

Related Article

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.